gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:00:25 24 4
gpt4 key购买 nike

我正在阅读 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 规范的 Angular 提出的(我希望它能得到回答),而不是从某些 JS 引擎实现的 Angular (在规范中可能会发生变化)。

最佳答案

(免责声明:我不代表 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.

关于javascript - React.addons.createFragment 对象中元素的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29663125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com