Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

As the title says: What is the difference between condensed arrays and literal arrays?

new Array("John", "Bob", "Sue"); // condensed array

["John", "Bob", "Sue"]; // literal array

Are there things I can do with one that I can't do with the other? Or is it the way it is kept in the memory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
337 views
Welcome To Ask or Share your Answers For Others

1 Answer

No, there is no difference in the produced object, they are the same.

The first is an attempt to satisfy programmers that are use to a "classical" environment where you have to instantiate a "new" Array object.

It should be noted that Arrays in javascript are not a sequential allocation of memory, but objects with enumerable property names and a few extra (useful) methods.

Because of this creating an array of a set length is fairly useless and unnecessary in most (if not all) cases.

var array = new Array(10);

is functionally the same is manually setting the length of your array

var array = [];
array.length = 10;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...