JavaScript的数组长度错误重新presentation [英] Wrong representation of JavaScript Array Length
问题描述
如果我有在JavaScript中的数组,说:
If I have an array in JavaScript, say:
var a = [1, 2, 3];
var b = ["Praveen", "Kumar", "Stack", "Overflow"];
如果我通过获取上述数组的长度:
If I get the lengths of the above arrays by:
a.length;
b.length;
我得到正确的值。即,
I get the correct values. i.e.,
a.length; // 3
b.length; // 4
但是,如果我创建另一个数组,其中设置我的下标,如:
But, if I create another array, where I set my indices like:
c = [];
c[5] = "Five";
c[10] = "Ten";
然后,如果我查询的长度,它显示我 11
。
c.length // 11
这是错的?或者是这样的JavaScript间$ P $点阵列?请指导。
Is this wrong? Or is this way JavaScript interprets arrays? Please guide.
推荐答案
的。长度
被定义为比最大的数字索引值大一。 (这不只是数字;它的32位整数,但基本上编号属性)
The .length
is defined to be one greater than the value of the largest numeric index. (It's not just "numeric"; it's 32-bit integer values, but basically numbered properties.)
相反,设置的的。长度
属性某些数值(比如 6
在你的例子)具有删除其属性名称是大于或等于你把它的值设置为若干属性的效果。
Conversely, setting the .length
property to some numeric value (say, 6
in your example) has the effect of deleting properties whose property name is a number greater than or equal to the value you set it to.
这篇关于JavaScript的数组长度错误重新presentation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!