为什么JavaScript中的数组显示错误的长度 [英] Why array in JavaScript showing wrong length

查看:105
本文介绍了为什么JavaScript中的数组显示错误的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Javascript。作为学习的一部分,我遇到了以下场景,我希望a1.length(代码的最后一行)显示201,但它显示101,任何想法?

I am learning Javascript. As a part of learning, I came across following scenario, where I expect the a1.length (the last line of the code) to show 201, but it shows 101, any Idea?

var a1 = new Array();

for (var i = -100; i<=100; i++)
 a1[i]  = i;

for (var i in a1)
{
    document.write(i + "=" + a1[i])
    document.write("<br>");
}

document.write(a1.length);


推荐答案

我会将原来的评论转换为更彻底回答。

I'll convert my original comment to a more thorough answer.

.length 计算的数组索引来自 0 及以上。负索引被视为对象的属性,而不是数组值。从下面的ECMAScript规范中可以看出,给定一些特殊处理,数组索引基本上只是某些类型的属性值。

Array indexes that are counted in .length go from 0 and up. Negative indexes are considered properties of the object, not array values. As you can see from the ECMAScript spec below, array indexes are essentially just certain types of property values given some special treatment.

来自 ECMAScript spec


15.4数组对象

数组对象对某类属性名称进行特殊处理。当且仅当ToString(ToUint32(P))等于P且ToUint32(P)不等于2 ^ 32时,属性名P(以String值的形式)是数组索引。属性名称为数组索引的属性也称为元素。每个Array对象都有一个length属性,其值始终是小于2 ^ 32的非负整数。 length属性的值在数值上大于名称为数组索引的每个属性的名称;无论何时创建或更改Array对象的属性,都会根据需要调整其他属性以保持此不变量。具体来说,每当添加名称为数组索引的属性时,如果需要,将更改length属性,使其大于该数组索引的数值;每当更改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 2^32. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32 . The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.






此外,你永远不应该迭代数组for-in-loop

for (var i in a1)

迭代 a1 的所有可枚举属性,其中包括所有数组索引,但可以还包括其他属性。如果你想只迭代 for 循环的数组元素,你应该使用另一种形式:

That iterates all enumerable properties of a1 which will include all array indexes, but could also include other properties. If you want to iterate only array elements with a for loop, you should use the other form:

for (var i = 0, len = a1.length; i < len; i++) 

打字稍微多一点,但更安全。

It is slightly more typing, but a lot safer.

或者,在更现代的浏览器中,您可以使用 .forEach() method

Or, in more modern browsers, you can use the .forEach() method.

这篇关于为什么JavaScript中的数组显示错误的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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