javascript中的对象变量是按照添加的顺序枚举的吗? [英] Are object variables in javascript enumerated in the order they were added?

查看:109
本文介绍了javascript中的对象变量是按照添加的顺序枚举的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

元素顺序 - 用于javascript中的(... in ...)循环

假设你有这样的代码:

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

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

是否保证按顺序打印1,2和3?我已经测试了,到目前为止情况已经如此,但我不知道它是否永远都是真的。有没有这样做的浏览器?没有什么奇怪的事情,比如删除东西,原型继承等。只是向对象添加属性。

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.

推荐答案

所有当前浏览器除了Chrome之外,Chrome会按照定义的顺序循环遍历对象的属性。

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

以下是Chrome错误报告: http://code.google.com/p/chromium/issues/detail?id=883 。它目前标记为 WontFix

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

这篇关于javascript中的对象变量是按照添加的顺序枚举的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆