javascript - JavaScript 数组是关联的吗?

标签 javascript arrays associative

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

最佳答案

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

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; ...

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


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

关于javascript - JavaScript 数组是关联的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575833/

相关文章:

javascript - 从对象列表获取唯一属性列表的快速方法

javascript - 具有重名的 JS 关联对象

javascript - VBScript 匿名关联数组? (就像 JavaScript 中一样)

Javascript:获取长度返回未定义

javascript - Ajax 和表单例份验证

C++ 指针行为异常

awk 以模式为索引的关联数组

php - $_POST 中没有任何内容。我的代码有问题吗?

javascript - 转发重定向到上一页

交换字符串中两个字符的C++程序