什么是之间和QUOT的差异;数组()"和" []"同时声明一个JavaScript数组?

什么是之间和QUOT的差异;数组()"和" []"同时声明一个JavaScript数组?

问题描述:

有什么声明的真正区别是这样一个数组:

What's the real difference between declaring an array like this:

var myArray = new Array();

var myArray = [];


有是有区别的,但是在该示例中没有差异。

There is a difference, but there is no difference in that example.

使用了更详细的方法:新的Array()确实有在参数中的一个额外的选项:如果你传递一个数字来构造函数,你会得到一个数组该长度:

Using the more verbose method: new Array() does have one extra option in the parameters: if you pass a number to the constructor, you will get an array of that length:

x = new Array(5);
alert(x.length); // 5

要说明不同的方法来创建一个数组:

To illustrate the different ways to create an array:

var a = [],            // these are the same
    b = new Array(),   // a and b are arrays with length 0

    c = ['foo', 'bar'],           // these are the same
    d = new Array('foo', 'bar'),  // c and d are arrays with 2 strings

    // these are different:
    e = [3]             // e.length == 1, e[0] == 3
    f = new Array(3),   // f.length == 3, f[0] == undefined

;