问题描述
我正在阅读 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):
但是,即将发布的ECMAScript版本(ES6/ES2015)实际上使迭代行为正式化(如果我正确理解规范的话).
However, the upcoming version of ECMAScript (ES6/ES2015) actually formalizes the iteration behavior (if I understand the spec correctly).
在规范中,它是关于对象的内部[[Enumerate]]
方法:
In the spec, it's said about an object's internal [[Enumerate]]
method:
- 让键成为一个新的空列表.
- 对于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对象中元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!