ES6是否引入了明确的对象属性枚举顺序? [英] Does ES6 introduce a well-defined order of enumeration for object properties?
问题描述
ES6是否为对象属性引入了明确的枚举顺序?
Does ES6 introduce a well-defined order of enumeration for object properties?
var o = {
'1': 1,
'a': 2,
'b': 3
}
Object.keys(o); // ["1", "a", "b"] - is this ordering guaranteed by ES6?
for(let k in o) {
console.log(k);
} // 1 2 3 - is this ordering guaranteed by ES6?
推荐答案
对于 for-in
和 Object.keys
:否。
对于某些其他操作:是,通常。
For some other operations: Yes, usually.
虽然ES6 / ES2015添加了属性顺序,但不需要 for-in
或 Object.keys
遵循该订单,由于旧版兼容性问题。
While ES6 / ES2015 adds property order, it does not require for-in
or Object.keys
to follow that order, due to legacy compatibility concerns.
for-in
循环根据 [[枚举]] ,其定义为(强调我的):
for-in
loops iterate according to [[Enumerate]], which [is defined as (emphasis mine):
当 O 的[[Enumerate]]内部方法被称为以下
步骤:
When the [[Enumerate]] internal method of O is called the following steps are taken:
返回一个Iterator对象( 25.1.1.2 ),其下一个方法在枚举的属性的所有String值的关键字中迭代
0 的。
Iterator对象必须从%IteratorPrototype%继承( 25.1.2 )。
枚举属性的机制和顺序不是
指定,但必须符合以下规定的规则 [1] 。
Return an Iterator object (25.1.1.2) whose next method iterates over all the String-valued keys of enumerable properties of O. The Iterator object must inherit from %IteratorPrototype% (25.1.2). The mechanics and order of enumerating the properties is not specified but must conform to the rules specified below [1].
ES7 / ES2016删除[[Enumerate]]内部方法,而是使用抽象操作 EnumerateObjectProperties ,但就像[[枚举]]一样,它没有指定任何订单。
ES7 / ES2016 removes the [[Enumerate]] internal method and instead uses the abstract operation EnumerateObjectProperties, but just like [[Enumerate]] it doesn't specify any order.
还可以从 Object.keys
:
如果一个实现定义了
for-in语句的特定的枚举顺序,则[...]
If an implementation defines a specific order of enumeration for the for-in statement, [...]
这意味着不需要实现来定义特定的枚举顺序。 ECMAScript项目编辑Allen Wirfs-Brock已经确认了这个 2015语言规范,在规范完成后发布的一篇文章。
That means implementations are NOT required to define a specific order of enumeration. This has been confirmed by Allen Wirfs-Brock, Project Editor of the ECMAScript 2015 Language Specification, in a post made after the specification was complete.
其他操作,如 Object.getOwnPropertyNames
, Object.getOwnPropertySymbols
和 Reflect.ownKeys
,请按照以下顺序执行普通对象:
Other operations, like Object.getOwnPropertyNames
, Object.getOwnPropertySymbols
and Reflect.ownKeys
, do follow the following order for ordinary objects:
- 整数索引(
- 其他字符串键(如果适用),在属性cre
- 在财产创建顺序中的符号键(如果适用)。
行为在 [[OwnPropertyKeys]] 内部方法。但请注意异国情调对象可能会定义该方法不同,例如
This behavior is defined in the [[OwnPropertyKeys]] internal method. But be aware exotic objects might define that method differently, e.g.
console.log(Reflect.ownKeys(new Proxy({}, {
ownKeys: () => ['3','1','2']
}))); // ['3','1','2'], the integer indices are not sorted!
[1] 下面说:
[[枚举]]必须通过调用其[[OwnPropertyKeys]]内部方法获取目标对象
的属性键。
[[Enumerate]] must obtain the own property keys of the target object as if by calling its [[OwnPropertyKeys]] internal method.
[[OwnPropertyKeys]]的顺序定义良好。但是不要让这个让你迷惑:那个好像只意味着同一个属性,而不是同一个命令。
And the order of [[OwnPropertyKeys]] is well-defined. But don't let that confuse you: that "as if" only means "the same properties", not "the same order".
一个href =http://www.ecma-international.org/ecma-262/6.0/#sec-enumerableownnames =noreferrer> EnumerableOwnNames ,它使用[[OwnPropertyKeys]]获取属性,然后按照与迭代器生成的相同的相对顺序来订购它们
This can be seen in EnumerableOwnNames, which uses [[OwnPropertyKeys]] to get the properties, and then it orders them
,如果
将返回,则返回
[[枚举]]内部方法被调用
in the same relative order as would be produced by the Iterator that would be returned if the [[Enumerate]] internal method was invoked
如果需要[[枚举]]以与[[OwnPropertyKeys ]],不需要重新排序。
If [[Enumerate]] were required to iterate with the same order as [[OwnPropertyKeys]], there wouldn't be any need to reorder.
这篇关于ES6是否引入了明确的对象属性枚举顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!