Chrome和可能的Opera自动排序对象属性 [英] Chrome and probably Opera sort object properties automatically

查看:114
本文介绍了Chrome和可能的Opera自动排序对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是:Chrome会自动对对象的属性进行排序。

Problem is: Chrome automatically sorts properties of object.

如果我有一个像这样的对象:

If I have an object like:

var obj = {4: "first", 2: "second", 1: "third"};

然后当我做下一个:

for(var i in obj) {
    console.debug(obj[i]);
}

我看下一个:

third
second
first

但预计:

首先
第二个
第三个

推荐答案

永远不要依赖属性的顺序。它们是无序的,没有规范定义应枚举的属性。

Never rely on the order of properties. They are unordered and there is no specification that defines in which order properties should be enumerated.

Chrome使用数字键以数字方式命令属性,而其他浏览器按插入顺序枚举它们。它取决于实现。

Chrome orders properties with numeric keys numerically, whereas other browsers enumerate them in insertion order. It is implementation dependent.

这篇关于Chrome和可能的Opera自动排序对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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