开发者

Are there any major browsers that do not preserve insertion order in a JavaScript Object?

Can I depend on t开发者_如何学Che following code alerting b before a?

var x = {}
x['b'] = 1
x['a'] = 0
for(var i in x) {
    alert(i)
}


Are there any major browsers that do not preserve insertion order in a JavaScript Object?

At least one major browser did until recently (I think the V8 engine didn't preserve order).

Can I depend on the following code alerting b before a?

No. The spec says that there is no order.


For the V8 JavaScript engine used in Google Chrome, a similar discussion took place:

http://code.google.com/p/v8/issues/detail?id=164

It's better to not rely on undocumented features. And it you're using numbers as keys, it certainly goes wrong.

For example this breaks in some browsers:

var x = {}
x['b'] = 1
x['2'] = 20
x['a'] = 0
x['1'] = 10
for(var i in x) {
    alert(x[i])
}

BTW it's alert(x[i]).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜