for-in evaluation order
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Mon Dec 27 03:45:52 PST 2010
On 27.12.2010 14:39, Michael Day wrote:
> 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
> 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.
More information about the es-discuss