本文介绍了React.addons.createFragment对象中元素的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 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:

[[OwnPropertyKeys]]被定义为

  1. 让键成为一个新的空列表.
  2. 对于O的每个自己的属性键P(它是整数索引),以数字索引的升序排列
  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. 添加P作为键的最后一个元素.

  • 对于每个自己的O的属性键P(按字符串创建,但不是整数索引),按属性创建顺序

  • For each own property key P of O that is a String but is not an integer index, in property creation order

    1. 添加P作为键的最后一个元素.

  • 对于每个自己的作为符号的O的属性键P(按属性创建顺序)

  • For each own property key P of O that is a Symbol, in property creation order

    1. 添加P作为键的最后一个元素.

  • 返回键.
  • Return keys.
  • 这篇关于React.addons.createFragment对象中元素的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-18 11:07
    查看更多