React.addons.createFragment对象中元素的顺序 [英] Order of elements in React.addons.createFragment object
问题描述
我正在阅读 https://facebook.github.io/react /docs/create-fragment.html 文章,发现FB工程师依赖于对象的内存布局(属性的顺序):
I'm reading the https://facebook.github.io/react/docs/create-fragment.html article and spotted that FB engineers rely on the object memory layout (the order of properties):
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
});
}
我是否缺少某些东西,或者它们依赖不可靠并提供易碎的代码?
Am I missing something or they rely on unreliable and provide the fragile code?
PS:这个问题是从ES规范的角度提出的(我希望可以回答),而不是从JS引擎实现的角度(可能会在规范内更改).
PS: The question is asked (and I expect it to be answered) from the ES specification point of view, not some JS engine implementation perspective (which is subject to change within spec).
推荐答案
(免责声明:我不是代表Facebook发言,这是我个人的观点)
(Disclaimer: I'm not speaking on behalf of Facebook, this is my own opinion)
您可能错过了此笔记(可能更占主导地位):
You may have missed this note (which could be a bit more predominant):
还请注意,此处我们依赖于JavaScript引擎保留对象枚举顺序,这不是规范所保证的,而是由所有主要的浏览器和VM针对具有非数字键的对象实现的.
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)实际上使迭代行为正式化(如果我正确理解规范的话).
However, the upcoming version of ECMAScript (ES6/ES2015) actually formalizes the iteration behavior (if I understand the spec correctly).
In the spec, it's said about an object's internal [[Enumerate]]
method:
[[Enumerate]]必须获得目标对象自己的属性键,就像通过调用其[[OwnPropertyKeys]]内部方法一样.
[[Enumerate]] must obtain the own property keys of the target object as if by calling its [[OwnPropertyKeys]] internal method.
调用O的[[OwnPropertyKeys]]内部方法时,将执行以下步骤:
When the [[OwnPropertyKeys]] internal method of O is called the following steps are taken:
- 让键成为一个新的空列表.
- 对于O的每个自己的属性键P(它是整数索引),以数字索引的升序排列
- Let keys be a new empty List.
- For each own property key P of O that is an integer index, in ascending numeric index order
- 添加P作为键的最后一个元素.
- 添加P作为键的最后一个元素.
- 添加P作为键的最后一个元素.
这篇关于React.addons.createFragment对象中元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!