What is the difference between condensed arrays and literal arrays?

后端 未结 3 1972
日久生厌
日久生厌 2020-12-14 09:55

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

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

[\"John\",          


        
3条回答
  •  醉酒成梦
    2020-12-14 10:20

    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;
    

提交回复
热议问题