什么是之间和QUOT的差异;数组()"和" []"同时声明一个JavaScript数组? [英] What’s the difference between "Array()" and "[]" while declaring a JavaScript array?

查看:162
本文介绍了什么是之间和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

;

这篇关于什么是之间和QUOT的差异;数组()"和" []"同时声明一个JavaScript数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆