像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
当前回答
事情远比看上去的复杂。大多数其他答案是正确的,但也…
新数组(n)
允许引擎为n个元素重新分配空间 为创建数组而优化 创建的数组被标记为稀疏数组具有最少的性能数组操作,这是因为每个索引访问都必须检查边界,查看是否存在值并遍历原型链 如果数组被标记为稀疏,就没有办法返回(至少在V8中),它在生命周期内总是会变慢,即使你在1毫秒或2小时后用内容填充它(打包的数组)也没关系
[1, 2, 3] | | []
创建的数组被标记为已打包(除非使用delete或[1,,3]语法) 针对数组操作进行了优化(用于.., forEach, map等) 随着数组的增长,引擎需要重新分配空间
对于旧版本的浏览器可能不是这样。
其他回答
有区别,但在那个例子中没有区别。
使用更详细的方法:new Array()在参数中有一个额外的选项:如果你将一个数字传递给构造函数,你将得到一个该长度的数组:
x = new Array(5);
alert(x.length); // 5
为了演示创建数组的不同方法:
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
;
另一个区别是,当使用new Array()时,您可以设置数组的大小,这会影响堆栈大小。如果你得到堆栈溢出(数组的性能。push vs array .unshift),当数组的大小超过堆栈的大小时,就会发生这种情况,并且必须重新创建。因此,根据用例,在使用new Array()时实际上可以提高性能,因为可以防止发生溢出。
正如在这个答案中指出的,new Array(5)实际上不会向数组中添加5个未定义的项。它只是为五个项目增加了空间。请注意,以这种方式使用Array会使依赖数组变得困难。用于计算的长度。
使用隐式数组创建数组与使用数组构造函数创建数组之间的区别很微妙,但很重要。
创建数组时使用
var a = [];
您正在告诉解释器创建一个新的运行时数组。根本不需要额外的处理。完成了。
如果你使用:
var a = new Array();
你告诉解释器,我想调用构造函数Array并生成一个对象。然后它通过执行上下文查找要调用的构造函数,并调用它,创建数组。
你可能会想“嗯,这一点都不重要。它们是一样的!”不幸的是,你不能保证。
举个例子:
function Array() {
this.is = 'SPARTA';
}
var a = new Array();
var b = [];
alert(a.is); // => 'SPARTA'
alert(b.is); // => undefined
a.push('Woa'); // => TypeError: a.push is not a function
b.push('Woa'); // => 1 (OK)
在上面的例子中,第一个调用将如您所期望的那样提醒“SPARTA”。第二个则不会。你最终会看到undefined。您还会注意到b包含所有原生Array对象函数,例如push,而另一个则不包含。
虽然您可能希望发生这种情况,但它只是说明了[]与new Array()不同的事实。
如果你只是想要一个数组,最好使用[]。我也不建议到处去重新定义Array…
为了更好地理解[]和new Array():
> []
[]
> new Array()
[]
> [] == []
false
> [] === []
false
> new Array() == new Array()
false
> new Array() === new Array()
false
> typeof ([])
"object"
> typeof (new Array())
"object"
> [] === new Array()
false
> [] == new Array()
false
以上结果来自Windows 7上的谷歌Chrome控制台。
事情远比看上去的复杂。大多数其他答案是正确的,但也…
新数组(n)
允许引擎为n个元素重新分配空间 为创建数组而优化 创建的数组被标记为稀疏数组具有最少的性能数组操作,这是因为每个索引访问都必须检查边界,查看是否存在值并遍历原型链 如果数组被标记为稀疏,就没有办法返回(至少在V8中),它在生命周期内总是会变慢,即使你在1毫秒或2小时后用内容填充它(打包的数组)也没关系
[1, 2, 3] | | []
创建的数组被标记为已打包(除非使用delete或[1,,3]语法) 针对数组操作进行了优化(用于.., forEach, map等) 随着数组的增长,引擎需要重新分配空间
对于旧版本的浏览器可能不是这样。
好吧,var x = new Array()与var x =[]在某些特性上是不同的,我只解释其中最有用的两个(在我看来)。
在我解释这些区别之前,我将首先设定一个基础;当我们使用x =[]时,定义了一个数据类型为Array的新变量,它继承了属于数组原型的所有方法,这与扩展一个类非常相似(但不完全相同)。但是,当我们使用x = new Array()时,它会初始化分配给变量x的数组原型的克隆。
现在让我们看看有什么不同
The First Difference is that using new Array(x) where x is an integer, initilizes an array of x undefined values, for example new Array(16) will initialize an array with 16 items all of them are undefined. This is very useful when you asynchronously fill an array of a predefined length. For example (again :) ) let's say you are getting the results of 100 competitiors, and you're receiving them asynchronously from a remote system or db, then you'll need to allocate them in the array according to the rank once you receive each result. In this very rare case you will do something like myArray[result.rank - 1] = result.name, so the rank 1 will be set to the index 0 and so on.
The second difference is that using new Array() as you already know, instanciates a whole new clone of the array prototype and assigns it to your variable, that allows you to do some magic (not recommended btw). This magic is that you can overwrite a specific method of the legacy array methods. So, for example you can set the Array.push method to push the new value to the beginning of the array instead of the end, and you can also add new methods (this is better) to this specific clone of the Array Prototype. That will allow you to define more complex types of arrays throughout your project with your own added methods and use it as a class.
最后一件事,如果你来自极少数关心应用程序处理开销和内存消耗的人(我真的很喜欢),你永远不会不顾一切地使用新Array():)。
我希望这已经足够解释野兽new Array():)