And I don't believe that anybody on this thread yet mentioned the impact of
inherited properties on enumeration order.
ES5 did not specify a an enumeration order because we discovered that there was
not a de facto for-in order that was used by all browser implementation. The
more edge cases that were examined, the more differences we found.
We did identify one situation where enumeration order will be the same across
all major implementation that are currently in use (including IE6):
The enumeration order of an object's properties will be the order in which the
properties were added if all the following conditions hold:
The object has no inherited enumerable properties
The object has no array indexed properties
No properties have been deleted
No property has had its attributes modified or been changed from a data
property to an accessor property or visa versa
In practice this means that if you create an object like:
var a = {};
a.y=1;
a.z=2;
a.x=3;
you can depend upon the enumeration order being '"y", "z", "x". If you do
anything more complex including using integer property names you can not depend
upon the order being the same across all browsers.
Any code that does is buggy.
Allen
On Dec 27, 2010, at 3:45 AM, Dmitry A. Soshnikov wrote:
> On 27.12.2010 14:39, Michael Day wrote:
>> Hi,
>>
>> Some quick testing with Firefox seems to show that for-in returns properties
>> in the order they were added to the object, and some scripts on the web (eg.
>> RaphaelJS) seem to depend on that, unless I'm horrendously misinterpreting
>> them. This seems... bad?
>>
>
> Yes, it's bad. Because the spec really statements that the order of
> evaluation isn't specified. Moreover, some side-effects, e.g. `delete`ing of
> properties or adding new is also not specified directly, though there is a
> mention on it. IIRC, there was a case with JScript (IE) and these
> side-effects. Regarding non-specificity enumeration, there was/is a case with
> V8 (Chrome).
>
> However, IMO, it's bad from the spec viewpoint (even if such a agreement is
> taken in price of optimization). I think (and sure many programmers will
> agree) that the consistent is more important. And the syntactic (lexical)
> appearing properties in the object should be enumerated in the same order.
> Adding/removing while enumeration also should be handled correctly.
>
>
>> Are objects implicitly ordered, by implementation convention?
>>
>
> In some implementations -- yes, in some -- not. So, it's not recommended to
> rely on it. Though, repeat, I'd change the spec instead.
>
> Dmitry.
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss