gpt4 book ai didi

javascript - JavaScript 和/或 JSON 是否需要解析器按定义顺序枚举属性?

转载 作者:数据小太阳 更新时间:2023-10-29 05:08:10 26 4
gpt4 key购买 nike

给定一个对象定义:

var o = {x :1, y:2, z: 3, b: 4, a: 5, m: 6, X: 7};

在枚举时,Chrome 似乎遵守属性定义的顺序:

for (var i in o) { console.log(i, o[i]); }

产量:

x 1
y 2
z 3
b 4
a 5
m 6
X 7

JavaScript 和/或 JSON 指定这种级别的顺序保留吗?

无论哪种情况,它可靠吗?

最佳答案

不,Javascript 规范明确要求任何特定的枚举顺序,它们根据定义是无序的。

参见 12.6.4 of the ECMAScript specification 部分:

The mechanics and order of enumerating the properties ... is not specified.

关于javascript - JavaScript 和/或 JSON 是否需要解析器按定义顺序枚举属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16971248/

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