4.2 Object and Array Initializers Object and array initializers are expressions whose value is a newly created object or array. These initializer expressions are sometimes called “ object literals ” and “ array literals.” Unlike true literals, however, they are not primary expressions, because they include a number of subexpressions that specify property and element values. Array initializers have a slightly simpler syntax, and we ’ ll begin with those. An array initializer is a comma-separated list of expressions contained within square brackets. The value of an array initializer is a newly created array. The elements of this new array are initialized to the values of the comma-separated expressions: [] // An empty array: no expressions inside brackets means no elements [1+2,3+4] // A 2-element array. First element is 3, second is 7 The element expressions in an array initializer can themselves be array initializers, which means that these expressions can create nested arrays: var matrix = [[1,2,3], [4,5,6], [7,8,9]];
The element expressions in an array initializer are evaluated each time the array ini- tializer is evaluated. This means that the value of an array initializer expression may be different each time it is evaluated.
淘宝翻译版: JavaScript 对数组初始化表达式进行求值得时候,数组初始化表达式中的元素表达式也都会各自计算一次。也就是说,数组初始化表达式每次计算的值都有可能是不同的.
问题: 1 ,为什么不同? 2 ,意味着什么?怎么样表现?有什么坑能举个栗子么?
Undefined elements can be included in an array literal by simply omitting a value be- tween commas. For example, the following array contains five elements, including three undefined elements: var sparseArray = [1,,,,5];
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.