Node.js库依靠对象键顺序是否可以接受? [英] Is it acceptable style for Node.js libraries to rely on object key order?

查看:37
本文介绍了Node.js库依靠对象键顺序是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枚举javascript对象的键会按插入顺序重播键:

Enumerating the keys of javascript objects replays the keys in the order of insertion:

> for (key in {'z':1,'a':1,'b'}) { console.log(key); }
z
a
b

这不是标准的一部分,但是已得到广泛实施(如此处):

This is not part of the standard, but is widely implemented (as discussed here):

ECMA-262没有指定枚举顺序.事实标准是要匹配广告订单,V8也这样做,但有一个例外:

ECMA-262 does not specify enumeration order. The de facto standard is to match insertion order, which V8 also does, but with one exception:

V8不保证数组索引的枚举顺序(即属性可以解析为32位无符号整数的名称.

V8 gives no guarantees on the enumeration order for array indices (i.e., a property name that can be parsed as a 32-bit unsigned integer).

在构建Node.js库时依靠这种行为是否可以接受?

推荐答案

绝对不是!这不是样式的问题,而是正确性的问题.

Absolutely not! It's not a matter of style so much as a matter of correctness.

如果您依赖此事实上的"标准,则您的代码可能会在ECMA-262第5版上失败.兼容的解释程序,因为该规范未指定枚举顺序.而且,V8引擎将来可能会改变其行为,例如出于性能方面的考虑

If you depend on this "de facto" standard your code might fail on an ECMA-262 5th Ed. compliant interpreter because that spec does not specify the enumeration order. Moreover, the V8 engine might change its behavior in the future, say in the interest of performance, e.g.

这篇关于Node.js库依靠对象键顺序是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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