Possible Duplicate:
What are the best practices to follow when declaring an array in Javascript?
Would be correct don't use new Array after of defining a variable using []?
var v = [];
v = Array(5); // is new necessary?
Possible Duplicate:
What are the best practices to follow when declaring an array in Javascript?
Would be correct don't use new Array after of defining a variable using []?
var v = [];
v = Array(5); // is new necessary?
The first line creates an empty array.
The second creates an array with 5 elements, all of which have the value undefined
.
There is no sense in using both of them. Which one to use depends on what you are trying to do, but the second form is not encountered very often in practice. It's very likely that the first is all you are going to need.
You are reassigning the value of v from a zero length array to an array of length 5. This makes the =[]
part redundant.
EDIT:
The major difference here is that the Array constructor's prototype can be modified, so what you get in the end is not necessarily identical to a standard issue javascript Array.
When you assign using [], an internal constructor is used, which means Array prototype tampering will not affect your new object. For example, I can set Array.prototype.join=function(){alert("duh");}
and all Arrays that you construct using new Array(...)
will alert "duh" when you try to call the join method. Arrays assigned using []
are immune to this.
No it is not necessary because both do the same thing. Basically all you should be doing is :
var v=[]; or var v=[value1, value2 ...];
The second version of defining an array has some issues like:
Array(4)
==> creates an empty 4-elements array
Array(4,5)
==> creates an array with 2 elements (4 and 5)
So if you want to create a 1 element array, it blows up in your face. Therefore, the first version is recommended.
The simple answer is that there's almost never a good reason to use the Array
keyword in a declaration, whether that's:
var a = Array(n)
or
var a = new Array(n)
just use:
var a = [];
instead.
Its not necessary to use new
with Array,i think you can create Array without using new
operator
You are creating two Array
objects and throwing away one, so what you are doing is not necessary.
Declare the variable and create an array:
var v;
v = new Array(5);
Or as a single statement:
var v = new Array(5);
The preferred way of creating arrays is using new Array
when you want to specify a size, and an array literal when you want to create an array from items:
var a1 = new Array(42); // creates an array with 42 undefined items
var a2 = [1, 2, 3]; // creates an array with the 3 items
For a zero size array you can use either:
var a1 = new Array(); // creates an empty array
var a2 = []; // creates an empty array
The Array
constructor can also take a list of items, but that use should be avoided because it behaves differently if there is one item and that item is numerical:
var a1 = new Array(100, 101, 102); // creates an array with three items
var a2 = new Array(100, 101); // creates an array with two items
var a3 = new Array(100); // creates an array with 100 undefined items
JavaScript - unlike most language - provides you arrays that kind of self-extend themselves when needed. But that doesn't mean it has no cost.
If you have an array of length N and put something at index M >= N, it has to create a new array of length M - 1, copy everything from N into it and then add your element, that's quite slow. Note that JavaScript has very diverse implementations and that some just consider arrays as normal objets with a special length property.
So to avoid that, when you know what size your array will be but not what it'll contain, you use new Array( size )
. If you know what it will contain, it's better to use the litteral notation [ item1, item2 ]
. And if you really don't know, just put []
.
added