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引擎不会分配一百万个元素.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...