Java Reference
In-Depth Information
constructedObject = new Object();
<< {}
A literal is still considered to be an instance of the Object constructor:
literal instanceof Object;
<< true
Similarly, the easiest way to create an array is to use the literal syntax like
so:
var literalArray = [1,2,3];
<< [1, 2, 3]
But an alternative is to use the Array constructor function:
constructedArray = new Array(1,2,3);
<< [1, 2, 3]
Array constructor functions exhibit some strange behavior regarding the ar-
guments supplied, however. If only one argument is given, it doesn't create
an array with that argument as the first element as you might expect; it sets
the array's length property instead!
new Array(5); // you might expect [5]
<< [undefined, undefined, undefined, undefined,
undefined]
This results in an error being thrown if a floating point decimal number is
provided as an argument because the length of an array must be an integer:
new Array(2.5);
<< Error: "invalid array length"
This behaviour is another reason why it is much better to use array literals
when creating arrays.
Search WWH ::




Custom Search