On Jun 30, 3:42 am, tobie <[EMAIL PROTECTED]> wrote: > > using "for ... in" to iterate through arrays > > is considered poor practice. > > Well, the for...in loop iterates over properties... so using it to > iterate over the members of an array is just using the wrong tool for > the job.
It isn't really an issue where all the code in a page is written by the same team, it is more likely to be a problem where code is mixed from different development teams or sources. There is rarely a need to use for..in with an Array and, as you say, it is generally considered bad practice. But the fact remains that it is perfectly legal to do so and there are a small number of cases where it is a good idea - sparse arrays in matrix calculations and where the properties of both Object and Array[1] are required in the same object are two. You can also use hasOwnProperty and propertyIsEnumerable methods to determine whether the property is on the object or on the prototoype chain. Even if you think you'll never have a problem with extended built-in objects, it is good to be aware of the fact that some of them have been extended even if only to know that there are some extra methods available and the need to avoid obvious traps. Also, to know not to add your own method that will clash with some already provided method. 1. Noting that javascript Arrays are Objects and there are other ways to solve this particular issue. -- Rob --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
