array like objects

Allen Wirfs-Brock Allen.Wirfs-Brock at microsoft.com
Tue Dec 8 11:18:16 PST 2009


-----Original Message-----
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Brendan Eich
...
>If anyone knows of real-world code that would be broken by the type of  
the for-in loop variable not always being string, I'm all ears.

>/be

I don't have a real world example or a compelling use case for the following, but it seems like it might (possibly inadvertently) occur on the web:

One place it makes a difference is if the + operator is used with the induction variable:

var a=[0,2,1];
var b=[];
for (var i in a) b[i+1] = a[i];

In ES3/5, b will end up with properties named '01', '11', '21' and length=22. Note that '01' is not an "array index" because of the leading '0'.
If for-in was changed so that i has numeric values for array index property names then b would end up with property names '1','2','3', and length=4.

The changed behavior is probably(??) what would be intended by somebody who did this, but changing it might still break apps that are currently perceived to be working.

My personal feeling is that rather than trying to "fix" various things about for-in (including pinning down enumeration order) it would be better to introduce a new more carefully defined enumeration statement(s) (for keys/for values??) and leave for-in alone.

Allen


More information about the es-discuss mailing list