Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

javascript - 元素按“ for(…in…)”循环排序(Elements order in a “for (… in …)” loop)

Does the "for…in" loop in Javascript loop through the hashtables/elements in the order they are declared?

(JavaScript中的“ for…in”循环是否按声明的顺序遍历哈希表/元素?)

Is there a browser which doesn't do it in order?

(有没有按顺序执行的浏览器?)


The object I wish to use will be declared once and will never be modified.

(我希望使用的对象将被声明一次,并且永远不会被修改。)

Suppose I have:

(假设我有:)

var myObject = { A: "Hello", B: "World" };

And I further use them in:

(我进一步将它们用于:)

for (var item in myObject) alert(item + " : " + myObject[item]);

Can I expect 'A : "Hello"' to always come before 'B : "World"' in most decent browsers?

(我是否可以期望在大多数体面的浏览器中,“ A:“ Hello””总是在“ B:“ World””之前?)

  ask by chakrit translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Quoting John Resig :

(引用John Resig :)

Currently all major browsers loop over the properties of an object in the order in which they were defined.

(当前,所有主流浏览器都按照定义它们的顺序遍历对象的属性。)

Chrome does this as well, except for a couple cases.

(Chrome可以做到这一点,除了少数情况。)

[...] This behavior is explicitly left undefined by the ECMAScript specification.

([...] ECMAScript规范明确未定义此行为。)

In ECMA-262, section 12.6.4:

(在ECMA-262中,第12.6.4节:)

The mechanics of enumerating the properties ... is implementation dependent.

(枚举属性的机制……取决于实现。)

However, specification is quite different from implementation.

(但是,规范与实现完全不同。)

All modern implementations of ECMAScript iterate through object properties in the order in which they were defined.

(ECMAScript的所有现代实现都按照定义它们的顺序遍历对象属性。)

Because of this the Chrome team has deemed this to be a bug and will be fixing it.

(因此,Chrome小组认为这是一个错误,并将予以修复。)

All browsers respect definition order with the exception of Chrome and Opera which do for every non-numerical property name.

(除了Chrome和Opera 以外,所有浏览器都遵循定义顺序,而Chrome和Opera则针对每个非数字属性名称。)

In these two browsers the properties are pulled in-order ahead of the first non-numerical property (this is has to do with how they implement arrays).

(在这两个浏览器中,属性是按顺序拉到第一个非数字属性之前的(这与它们实现数组的方式有关)。)

The order is the same for Object.keys as well.

(Object.keys的顺序也相同。)

This example should make it clear what happens:

(此示例应清楚说明发生了什么:)

var obj = {
  "first":"first",
  "2":"2",
  "34":"34",
  "1":"1",
  "second":"second"
};
for (var i in obj) { console.log(i); };
// Order listed:
// "1"
// "2"
// "34"
// "first"
// "second"

The technicalities of this are less important than the fact that this may change at any time.

(它的技术性不如可能随时更改的事实重要。)

Do not rely on things staying this way.

(不要依赖保持这种状态的事物。)

In short: Use an array if order is important to you.

(简而言之: 如果顺序对您很重要,请使用数组。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...