没有想过这个问题,不过你说的这个有意义吗? var a4 = [1,,3,]; a4[4]=4; 我如果这样写,都没有问题,数组的长度会自己增加,所以,不管那个是否存在,都可以进行操作的,不会出错,这应该是若语言的关系吧,就像js会自动根据需求转换类型。不懂~~
知道有这个情况,但真心不懂为啥,也许是因为for in更适合是对象的操作吧~~
http://oreilly.com/catalog/errata.csp?isbn=9780596805531quote from Ch. 7.3 p. 144 >>>"Note that when you omit value in an array literal, you are not creating a sparse array. The omitted element exists in the array and has the value undefined. This is subtly different than array elements that do not exist at all. You can detect the difference between these two cases with the in operator:var a1 = [,,,]; // This array is [undefined, undefined, undefined] var a2 = new Array(3); // This array has no values at all 0 in a1 // => true: a1 has an element with index 0 0 in a2 // => false: a2 has no element with index 0"<<< end of quoteHowever, v8 implementation of this differs from what the book suggests; both "0 in a1" and "0 in a2" are false. Also, the developers say v8 follows the ECMAScript specification. If that is true, the quoted text is erroneous.The issue is discussed in the following v8 bug which also contains a quote from the ECMAScript specification:http://code.google.com/p/v8/issues/detail?id=1371Note from the Author or Editor: Resolution: Yuck! The 4th paragraph of page 144 plus the 4 lines of code after it plus the one line paragraph after the code all have to be removed and replaced with the following: Note that when you omit a value in an array literal (using repeated commas as in <literal>[1,,3]</literal>) the resulting array is sparse and the omitted elements simply do not exist: var a1 = [,]; // This array has no elements and length 1 var a2 = [undefined]; // This array has one undefined element 0 in a1 // => false: a1 has no element with index 0 0 in a2 // => true: a2 has the undefined value at index 0 Some older implementations (such as Firefox 3) incorrectly insert the <literal>undefined</literal> values in array literals with omitted values. In these implementations <literal>[1,,3]</literal> is the same as <literal>[1,undefined,3]</literal>.Also, on page 142, the 3rd paragraph and the two lines that follow and the paragraph after that must be changed as follows: If an array literal contains multiple commas in a row, with no value between, the array is sparse (see 7.3). Array elements for which values are omitted do not exist, but appear to be <literal>undefined</literal> if you query them: var count = [1,,3]; // Elements at indexes 0 and 2. count[1] => undefined var undefs = [,,]; // An array with no elements but a length of 2 Array literal syntax allows an optional trailing comma, so <literal>[,,]</literal> has a length of 2, not 3.Hopefully these two changes sort of balance each other out and do not cause too many page break changes. Let me know if you need to shorten or lengthen them.
a1为空数组
a2有两个元素,均为undefined
其他的类似
呃,在数组中.length就是等于元素个数
var undefs = [,,]; //数组有2个元素,都是undefined
数组直接量的语法允许有可选的结尾的逗号
如果数组是稀疏的,length属性值大于元素的个数
最后一个逗号后面为空的话,是不算入的。这样有错吗?
var a4 = [1,,3,];
a4[4]=4;
我如果这样写,都没有问题,数组的长度会自己增加,所以,不管那个是否存在,都可以进行操作的,不会出错,这应该是若语言的关系吧,就像js会自动根据需求转换类型。不懂~~
var a2 = new Array(3); // This array has no values at all
0 in a1 // => true: a1 has an element with index 0
0 in a2 // => false: a2 has no element with index 0"<<< end of quoteHowever, v8 implementation of this differs from what the book suggests; both "0 in a1" and "0 in a2" are false. Also, the developers say v8 follows the ECMAScript specification. If that is true, the quoted text is erroneous.The issue is discussed in the following v8 bug which also contains a quote from the ECMAScript specification:http://code.google.com/p/v8/issues/detail?id=1371Note from the Author or Editor:
Resolution: Yuck! The 4th paragraph of page 144 plus the 4 lines of
code after it plus the one line paragraph after the code all have to be
removed and replaced with the following: Note that when you omit a value in an array literal (using repeated
commas as in <literal>[1,,3]</literal>) the resulting array is sparse
and the omitted elements simply do not exist: var a1 = [,]; // This array has no elements and length 1
var a2 = [undefined]; // This array has one undefined element
0 in a1 // => false: a1 has no element with index 0
0 in a2 // => true: a2 has the undefined value at index 0 Some older implementations (such as Firefox 3) incorrectly insert the
<literal>undefined</literal> values in array literals with omitted
values. In these implementations <literal>[1,,3]</literal> is the
same as <literal>[1,undefined,3]</literal>.Also, on page 142, the 3rd paragraph and the two lines that follow and
the paragraph after that must be changed as follows: If an array literal contains multiple commas in a row, with no value
between, the array is sparse (see 7.3). Array elements for which
values are omitted do not exist, but appear to be
<literal>undefined</literal> if you query them: var count = [1,,3]; // Elements at indexes 0 and 2. count[1] => undefined
var undefs = [,,]; // An array with no elements but a length of 2 Array literal syntax allows an optional trailing comma, so
<literal>[,,]</literal> has a length of 2, not 3.Hopefully these two changes sort of balance each other out and do not
cause too many page break changes. Let me know if you need to shorten
or lengthen them.