create 3 undefined, empty array.
var a1 = [,,,];
var a2 = new Array(3);
from JavaScript: The Definitive Guide,
0 in a1 //true
0 in a2 //false
but, in real world browser, getting different result. (IE8 and chrome 33...)
0 in a1 //false
0 in a2 //false
which is true, book or real world?
create 3 undefined, empty array.
var a1 = [,,,];
var a2 = new Array(3);
from JavaScript: The Definitive Guide,
0 in a1 //true
0 in a2 //false
but, in real world browser, getting different result. (IE8 and chrome 33...)
0 in a1 //false
0 in a2 //false
which is true, book or real world?
Share Improve this question edited Jan 23, 2014 at 0:45 Composite asked Jan 23, 2014 at 0:32 CompositeComposite 1654 silver badges16 bronze badges 9-
2
Looks like the book is wrong. As you can see from the spec, es5.github.io/#x11.1.4,
[,,,]
does not add any values to the array. – Felix Kling Commented Jan 23, 2014 at 0:34 -
2
@user2864740: No,
[,,,]
is **not**[undefined,undefined,undefined,undefined]
. – Felix Kling Commented Jan 23, 2014 at 0:38 - @FelixKling it is undefined – Mina Gabriel Commented Jan 23, 2014 at 0:41
-
1
@MinaGabriel: Accessing a non-existing property returns undefined, but it's not the same has adding
undefined
to the array. Just try0 in [,]
and0 in [undefined]
. – Felix Kling Commented Jan 23, 2014 at 0:47 - 1 @PatrickEvans: ECMAScript5 also exists a couple of years already and that behavior was already defined in ECMAScript3. – Felix Kling Commented Jan 23, 2014 at 0:48
2 Answers
Reset to default 7Looks like the book is wrong. As you can see from the specification, [,,,]
does not add any values to the array:
The production ArrayLiteral : [ Elisionopt ] is evaluated as follows:
- Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name.
- Let pad be the result of evaluating Elision; if not present, use the numeric value zero.
- Call the
[[Put]]
internal method of array with arguments "length", pad, and false.- Return array.
("Elisions" are the ,
which are not preceded or followed by an expression.)
In simpler terms:
- Create an empty array
- Evaluate the
,
, which is basically just counting them, starting from 1. So,,,
results in 3. - Then set the
length
of the array to the result (e.g. 3).
And that's exactly what new Array(3)
is doing as well.
There is also a less formal description of elided elements:
Array elements may be elided at the beginning, middle or end of the element list. Whenever a ma in the element list is not preceded by an AssignmentExpression (i.e., a ma at the beginning or after another ma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.
The Array constructor (new Array( 3 )
) does set the length
attribute to 3 but does not create any members of the array – not even undefined values.
In cases when there is only one argument passed to the Array constructor and when that argument is a Number, the constructor will return a new sparse array with the length property set to the value of the argument. It should be noted that only the length property of the new array will be set this way; the actual indexes of the array will not be initialized.
Source: http://bonsaiden.github.io/JavaScript-Garden/#array.constructor
Testing that in Chromium does indeed result in two false values…
The in
operator returns false if the given key does not exist:
var a1 = [1,,3,4];
0 in a1 // true
1 in a1 // false
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742383008a4433495.html
评论列表(0条)