Why are there no ordered dictionaries?
Duncan Booth
duncan.booth at invalid.invalid
Thu Nov 24 03:49:36 EST 2005
Christoph Zwerschke wrote:
> Duncan Booth schrieb:
>> In Javascript Object properties (often used as an associative array)
>> are defined as unordered although as IE seems to always store them in
>> the order of original insertion it wouldn't surprise me if there are
>> a lot of websites depending on that behaviour.
>
> You're right with both. The ECMA language definition says object
> properties are an unordered collection, but MSIE and probably other
> browsers keep the order in which they were created. Of course one
> should not rely on that.
>
Yes, the real fun bit is arrays. If you create an array using the 'new
Array' or [ ... ] then a for..in loop might well trick you into thinking it
is going to go through the elements in order, but if you assign to elements
directly it breaks down:
a = ['a', 'b', 'c'];
a[4] = 'e';
a[3] = 'd';
for (var k in a) {
alert(a[k]+'='+a[k]);
}
On IE this will go through elements in the order 0, 1, 2, 4, 3.
Also, it is original order of insertion which matters, not the 'last
in/last out' you might have expected. In other words it looks rather as
though IE simply sets deleted properties to undefined (and skips them on
iteration), it never really deletes a property, so anyone who tries to use
a Javascript object as an associative array with lots of rapidly changing
keys could be in for a nasty shock.
More information about the Python-list
mailing list