On Mar 18, 8:22 am, Diego Perini <[email protected]> wrote:
> On Thu, Mar 17, 2011 at 3:01 AM, RobG <[email protected]> wrote:
>
> > On Mar 17, 10:47 am, RobG <[email protected]> wrote:
> >> On Mar 17, 10:40 am, RobG <[email protected]> wrote:
> >> [...]
>
> >> >  Try this in various browsers:
>
> >> > (function() {
> >> [...]
> >> > })();
>
> >> Here's one I forgot to add:
>
> >>   // How about an array object
> >>   var arr = [];
> >>   arr[1] = 'one';
> >>   arr[0] = 'two';
> >>   show('Is an array in order?\n', arr);
>
> >> As expected, some return the properties in index order, others in the
> >> order they are added. So *please* do not encourage anyone to think
> >> properties will be returned in the order they are added because they
> >> aren't, nor are they required to be, even in the latest ECMAScript
> >> edition.
>
> > And another:
>
> >  // Add some properties in order
> >  obj['0'] = 0;
> >  obj.first = 'first';
> >  obj.second = 'second';
> >  obj.third = 'third';
> >  obj.fourth = 'fourth';
> >  obj['5'] = 5;
>
> > Chrome 10 and Opera 11 return them in the following order:
>
> >  0,5,first,second,third,fourth
>
> Do they get performances benefits on the lookups by reordering members
> this way ?

I don't know, you'd need to ask the developers why they wrote it that
way.

My guess is that they treat the properties of arrays and objects the
same, so if you use for..in on an array you might expect only numeric
indexes, so they put them first and in order regardless of when they
were added. When iterating over an object you probably only expect
alphabetic indexes, so they are returned in the order they were added.
I suppose they didn't expect them mixed together.

My main goal was to disprove the assertion that "you can count on ...
any browser" returning properties in the order they were added. For
each browser I tested, it can be shown that in certain cases they do
not.

If reading object properties in a specific order matters, then an
index should be used.

Those who propose that object properties be returned in the order they
were added also need to think through other scenarios:

1. Should for..in iteration over an array return the indexes in the
order they were added, or numeric? Note that there is lots of code
that uses a reverse counter when adding members to an array using
loops like:

    while (i--) ...

2. Should this behaviour extend to host objects (i.e. should it be
added to the W3C DOM Core or HTML5)?

3. If a property is removed and re-added, does it go back to its
original position or does it go last?

4. Can keys be re-ordered in some manner other than removing them all,
re-ordering them, then adding them again? e.g. to inserting a key
between two others


--
Rob

-- 
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]

Reply via email to