微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaScript数组是否关联?

例如,如果我这样做
一个[百万] = 1;
它会使用1000000个元素的内存还是只用于这个?

解决方法

在ECMAScript标准(第15.4节)中,关于数组的唯一特殊之处在于自动更新了length属性(以及一堆特定于Array的原型函数):

Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 232−1.

Every Array object has a length property whose value is always a nonnegative integer less than 232. The value of the length property is numerically greater than the name of every property whose name is an array index; …

除此之外,Array只是一个Object,这意味着它可以被视为一个关联数组,although you shouldn’t.

现在,JS引擎应检测阵列是密集还是非常稀疏,并在内部使用线性或关联数组之间切换.在您的情况下,JS引擎不会分配一百万个元素.

原文地址:https://www.jb51.cc/js/155431.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐