for-in evaluation order
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Mon Dec 27 04:23:35 PST 2010
On 27.12.2010 15:18, Andreas Gal wrote:
> Note that almost all implementations break for-in enumeration order for dense arrays, for some definition of dense.
>
> Example (firefox):
>
> a = []
> a[2] = 1
> a[1] = 1
> a[0] = 1
> for (i in a) alert(i)
>
> will alert 0, 1, 2 (in that order).
>
> Now if you do
> a["x"] = 1
>
> you get 0, 1, 2, "x"
>
> However, if we start out with x, we get this:
>
> a = []
> a["x"] = 1
> a[2] = 1
> a[1] = 1
> a[0] = 1
>
> Will alert x, 2, 1, 0
>
Hm, interesting (didn't know about SpiderMonkey), thanks.
> Even worse, this is likely to change in the future (we want to give all objects a dense, indexed part).
>
> Most browser behave similar with respect to indexed properties. For good performance you really want to ignore enumeration order for array-like objects, but if the implementation falls back onto regular objects, you can suddenly observe true property addition order.
>
Yep, indeed, with a = {} the order is as was specified in the source.
> If we try to specify something, implementors will probably object, and each implementation will have specific objections depending on their particular array and object representation.
>
It's interesting. Could you summarize these objections?
Dmitry.
> Andreas
>
> On Dec 27, 2010, at 12:52 PM, Michael Day wrote:
>
>> Thanks Boris, Dmitry,
>>
>>>> Are objects implicitly ordered, by implementation convention?
>>> For implementations that have to deal with the web, yes.
>> Okay, now I know what we have to do then :)
>>
>> Might I suggest that this be added to the spec, if only in an informative note, to save future implementers the trouble?
>>
>> Best regards,
>>
>> Michael
>>
>> --
>> Print XML with Prince!
>> http://www.princexml.com
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list