Are object variables in javascript enumerated in t

2019-07-23 14:26发布

问题:

Possible Duplicate:
Elements order - for (... in ...) loop in javascript

Assume you have code like this:

var a = {}
a.a = 1;
a.c = 2;
a.b = 3;

for (var i in a) {
    console.log(a[i]);
}

Are 1, 2, and 3 guaranteed to be printed in that order? I've tested and this has been the case so far, but I don't know if it'll always be true. Is there any browser which does not do this? There's nothing weird going on, like deleting things, prototype inheritance, etc. Just adding properties to an object.

回答1:

All current browsers with the exception of Chrome will loop over the properties of an object in the same order as they were defined.

Here is the chrome bug report: http://code.google.com/p/chromium/issues/detail?id=883. It is currently marked as WontFix.



回答2:

In my current version of Chrome (2.0.172.28) John Resig's test case passes, so maybe it is fixed in Chrome now?