javascript - 大数字索引的Object.keys顺序?

标签 javascript ecmascript-6

我有这个测试:

let obj = {
    "5555555555": 5,
    "4444444444": 4,
    "3333333333": 3,
    "2222222222": 2,
};
console.log(Object.keys(obj));


输出为["2222222222", "3333333333", "5555555555", "4444444444"]
但是,EcmaScript 2015规范定义了[[OwnPropertyKeys]]的顺序:

  1. For each own property key P of O that is an integer index, in ascending numeric index order,
        a. Add P as the last element of keys.


整数索引是defined as:

An integer index is a String-valued property key that is a canonical numeric String and whose numeric value is either +0 or a positive integer ≤ 253−1. An array index is an integer index whose numeric value i is in the range +0 ≤ i < 232−1.



由于上面片段中的obj中的所有四个属性都是按定义定义的整数索引,因此当我调用Object.keys时,为什么它们没有按数字顺序列出?

我在Chrome,Firefox,Edge,甚至Internet Explorer 11中都看到了相同的顺序。似乎它们对数组索引而不是(所有)整数索引应用了“升序数字索引顺序”。只有这样才能解释为什么1111111111和2222222222首先按数字顺序排序(仅有两个键<232-1),而4444444444和5555555555最后按其原始相对顺序排序。

我想念什么?这些实现是否违反规范?

最佳答案

没有任何主要实现符合该特定的ES6规范。但请注意,EcmaScript 2015-2019中允许异常(exception)(请参阅this)
关于所描述的行为,例如,参见V8错误报告Issue 7874: Respect OrdinaryOwnPropertyKeys order
尽管EcmaScript 2016、2017和2018规范在这方面保持不变,但已决定将EcmaScript2019与现实保持一致。 [[OwnPropertyKeys]]()上的部分现在显示为:

  1. For each own property key P of O that is an array index, in ascending numeric index order, do
        a. Add P as the last element of keys.

因此,引用的是数组索引,该索引的定义没有更改(+0≤i <232-1)。
此更改发生在拉取请求Normative: Use array indices instead of integer indices in OrdinaryOwnPropertyKeys中。
另请参见此处的注释和交叉引用。

关于javascript - 大数字索引的Object.keys顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60121521/

相关文章:

javascript - 从另一个商店 mobx 调用商店

javascript - 如何使用reduce、解构和递归检查数组相等性?

javascript - 管理大型表单 - ReactJS

javascript - 检查 javascript 中的空表单元素

javascript - 选择后删除搜索字段中的文本

javascript - Page_ClientValidate() 问题

javascript - 在父组件中使用组件的多个实例

javascript - 如何使用map函数在第一个react组件中添加图像并忽略其余组件

javascript - 纯 javascript 到 jquery - clientHeight

javascript - MEAN 应用程序中 UI 路由器上的嵌套 View