大数字索引的Object.keys顺序? [英] Object.keys order for large numerical indexes?

查看:109
本文介绍了大数字索引的Object.keys顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个测试:

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

输出为["2222222222", "3333333333", "5555555555", "4444444444"]

但是,EcmaScript 2015规范定义了

However, the EcmaScript 2015 specification defines the order for [[OwnPropertyKeys]]:

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

整数索引定义为:

整数索引是一个字符串值的属性键,它是规范的数字字符串 并且其数值为+0或正整数≤2 53 -1. 数组索引是整数索引,其数值 i 的范围是+0≤ i < 2 32 −1.

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中的 all 四个属性都是按定义定义的整数索引,当我调用Object.keys时,为什么它们没有按数字顺序列出?

As all four properties in obj, in the snippet above, are integer indexes by definition, how come they are not listed in numerical order when I call Object.keys?

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

I see this same order in Chrome, Firefox, Edge, even Internet Explorer 11. It seems like they apply the "ascending numeric index order" for array indexes, not (all) integer indexes. Only that can explain why 1111111111 and 2222222222 are ordered first and in numerical order (the only two keys that are < 232-1), and 4444444444 and 5555555555 are ordered last in their original relative order.

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

What am I missing? Are these implementations violating the specs?

推荐答案

没有一个主要实现符合该特定的ES6规范.但请注意,EcmaScript 2015-2019中允许例外(请参见

None of the major implementations had ever been in line with that particular ES6 specification. But note that exceptions were allowed in EcmaScript 2015-2019 (see this)

关于您注意到的行为,例如,参见V8错误报告问题7874:遵守OrdinaryOwnPropertyKeys顺序.

Concerning the behaviour you note, see for instance the V8 bug report Issue 7874: Respect OrdinaryOwnPropertyKeys order.

尽管EcmaScript 2016、2017和2018规格在这方面保持不变,但已决定使EcmaScript2019与现实保持一致.关于

While EcmaScript 2016, 2017 and 2018 specifications remained unchanged in this respect, it was decided to align EcmaScript2019 with reality. The section on [[OwnPropertyKeys]]() now reads:

  1. 对于作为数组索引的 O 的每个自己的属性键 P ,按数字索引升序执行    一种.添加 P 作为的最后一个元素.
  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 <2 32 -1).

So reference is to array index, which has an unchanged definition (+0 ≤ i < 232−1).

此更改发生在请求请求>规范中:在OrdinaryOwnPropertyKeys中使用数组索引而不是整数索引. 另请参阅此处的评论和交叉引用.

This change occurred in pull request Normative: Use array indices instead of integer indices in OrdinaryOwnPropertyKeys. See also the comments and cross-references there.

这篇关于大数字索引的Object.keys顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆