On Fri, Mar 11, 2011 at 9:58 PM, Alex <[email protected]> wrote: > I'm struggling to find something very simple. > > Question 1: > If I have an array of integers and I use a for-in construct, do I get > them in the same order they are present inside array? >
No. In fact, for-in is not meant to iterate an array but will instead iterate all "properties" of the array. Note that an array is nothing more than a regular object with some special stuff to it. Hence, for-in will treat it exactly the same as any other object and will not only return you the index properties (0, 1, 2, etc) but also other properties (like length) and inherited properties (like prototype's push, pop, etc). So, never use for-in to iterate over an array unless this was what you wanted. Always use for (var i ... ++i) or the array lambda family (forEach, map, filter, etc). (Note that the order of for-in might look stable but is not specified to be stable. There's some discussion to change that right now but for the time being, don't rely on it) Question 2: > If you can use both first and second construct does it means that the > indices of the vector are both properties (of object Array) and > indices of the array? > Yes. An index property is a normal property on the array. In fact, there's no difference in setting arr[5] or arr["5"]. - peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
