React.addons.createFragment 对象中元素的顺序

Order of elements in React.addons.createFragment object

我正在阅读 https://facebook.github.io/react/docs/create-fragment.html 文章,发现 FB 工程师依赖于对象内存布局(属性的顺序):

if (this.props.swapped) {
  children = React.addons.createFragment({
    right: this.props.rightChildren,
    left: this.props.leftChildren
  });
} else {
  children = React.addons.createFragment({
    left: this.props.leftChildren,
    right: this.props.rightChildren
  });
}

我是不是遗漏了什么,或者他们依赖不可靠并提供了脆弱的代码?

PS: 这个问题是从 ES 规范的角度提出的(我希望它能得到回答),而不是从某些 JS 引擎实现的角度(在规范中可能会发生变化)。

(免责声明:我不代表 Facebook,这是我自己的观点)

您可能错过了这个注释(可能更重要):

Note also that we're relying on the JavaScript engine preserving object enumeration order here, which is not guaranteed by the spec but is implemented by all major browsers and VMs for objects with non-numeric keys.

然而,即将发布的 ECMAScript 版本 (ES6/ES2015) 实际上正式化了迭代行为(如果我对规范的理解正确的话)。

在规范中,提到了 object's internal [[Enumerate]] method:

[[Enumerate]] must obtain the own property keys of the target object as if by calling its [[OwnPropertyKeys]] internal method.

And [[OwnPropertyKeys]] is defined as

When the [[OwnPropertyKeys]] internal method of O is called the following steps are taken:

  1. Let keys be a new empty List.
  2. For each own property key P of O that is an integer index, in ascending numeric index order
    1. Add P as the last element of keys.
  3. For each own property key P of O that is a String but is not an integer index, in property creation order
    1. Add P as the last element of keys.
  4. For each own property key P of O that is a Symbol, in property creation order
    1. Add P as the last element of keys.
  5. Return keys.