Hi, > I'm missing something here.
One of us is. :-) The OP's problem was that using for..in on an array was looping through the property names and he expected it to loop through array indexes. That's the common misconception I was talking about. People think for..in loops through array indexes, which is just plain wrong. While it *does* that (because array indexes are property names), it's not limited to doing that; it loops through all enumerable properties (as you know). All will appear to be well in a vanilla environment with a straightforward array instance, but not when either the Array.prototype or the array instance has had other enumerable properties added to it. I think if you read the wiki article[1] I pointed him at (I wrote it, a couple of years back) you'll see what I'm saying more clearly -- though since you clearly understand what for..in does, it may not be a worthwhile way to spend your time. The article is for people like the OP who aren't that familiar with JavaScript's arrays and the for..in construct. [1] http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays -- T.J. On Aug 3, 1:10 am, RobG <[email protected]> wrote: > On Aug 3, 2:19 am, "T.J. Crowder" <[email protected]> wrote: > > > Hi, > > > > > Yes, it's an extremely common misconception. > > > > What, that for..in iterates over an object's properties? > > > No, that it loops over array indexes. > > I'm missing something here. Array indexes are properties, which is why > they are included in for..in iteration. The OPs problem was how to > deal with properties from the [[prototype]] chain, not that array > properties were included in for..in. > > There are two issues to deal with when using for..in with a javascript > array: > > 1. properties may have been added to Array.prototype, and > > 2. object properties are not guaranteed to be returned in any > particular order > > Both the above are common to any use of for..in. Item 1 is easily > dealt with. If order matters, a for, while or do loop can be used. > Otherwise, there is no issue. > > -- > Rob -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" 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/prototype-scriptaculous?hl=en.
