当 Array 元素中的逗号不会影响 Array 的长度时
when the a comma in the Array elements will not contributes to the length of the Array
Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements.
但是
var a = [b=1,,]
console.log(a.length) //2
逗号前面是 AssignmentExpression (b=1),缺少的数组元素贡献了 length.i 想知道为什么吗?
ECMAScript 表示不提供长度。我想知道为什么逗号可以不贡献长度,除了在数组的末尾
一个逗号 跟在 一个被计数的元素之后,它不标记一个的开始。
数组中最后一个逗号是没有意义的,因为前面的元素已经被考虑在内。
你遗漏了解释的最后一句,这似乎回答了你的问题:
If an element is elided at the end of an array, that element does not contribute to the length of the Array.
它还说数组开头或中间的空元素 do 有助于数组长度(因为它们 follow 计数元素,即使它们是空的)。
如果你只有这个:[,]
你的长度将是 1,因为数组开头 逗号之前是空的未定义元素。
你的例子:
var a = [b=1,,]
只有 2 个元素而不是 3 个,因为第二个逗号后没有任何内容。第一个逗号之后的空元素确实有所贡献,因为它在末尾是 not 并且后面跟着第二个逗号,表明那里有一个索引,但其中什么也没有。就像写[b=1, undefined]
一样。第二个逗号实际上是一个错字。
Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements.
但是
var a = [b=1,,]
console.log(a.length) //2
逗号前面是 AssignmentExpression (b=1),缺少的数组元素贡献了 length.i 想知道为什么吗?
ECMAScript 表示不提供长度。我想知道为什么逗号可以不贡献长度,除了在数组的末尾
一个逗号 跟在 一个被计数的元素之后,它不标记一个的开始。 数组中最后一个逗号是没有意义的,因为前面的元素已经被考虑在内。
你遗漏了解释的最后一句,这似乎回答了你的问题:
If an element is elided at the end of an array, that element does not contribute to the length of the Array.
它还说数组开头或中间的空元素 do 有助于数组长度(因为它们 follow 计数元素,即使它们是空的)。
如果你只有这个:[,]
你的长度将是 1,因为数组开头 逗号之前是空的未定义元素。
你的例子:
var a = [b=1,,]
只有 2 个元素而不是 3 个,因为第二个逗号后没有任何内容。第一个逗号之后的空元素确实有所贡献,因为它在末尾是 not 并且后面跟着第二个逗号,表明那里有一个索引,但其中什么也没有。就像写[b=1, undefined]
一样。第二个逗号实际上是一个错字。