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

var myArray = new Array();

and

var myArray = [];

当前回答

当你初始化没有任何长度的数组时,没有区别。因此var a = [] & var b = new Array()是相同的。

但是如果你初始化数组的长度像var b = new array(1);,它将数组对象的长度设置为1。所以它等价于var b = [];b.length = 1;。

当你使用array_object时,这就会有问题。按下,它在最后一个元素后添加项目并增加长度。

var b = new Array(1);
b.push("hello world");
console.log(b.length); // print 2

vs

var v = [];
a.push("hello world");
console.log(b.length); // print 1

其他回答

使用的区别

var arr = new Array(size);

Or

arr = [];
arr.length = size;

在这个问题上已经讨论得够多了。

我想添加速度问题-目前最快的方式,在谷歌chrome是第二个。

但请注意,这些东西往往会随着更新而发生很大变化。此外,不同浏览器的运行时间也不同。

例如-我提到的第二个选项,在chrome上运行200万[ops/秒],但如果你在mozilla dev上尝试,你会得到一个惊人的更高的速度2300万。

无论如何,我建议你每隔一段时间在不同的浏览器(和机器)上检查一下,使用站点本身

当你初始化没有任何长度的数组时,没有区别。因此var a = [] & var b = new Array()是相同的。

但是如果你初始化数组的长度像var b = new array(1);,它将数组对象的长度设置为1。所以它等价于var b = [];b.length = 1;。

当你使用array_object时,这就会有问题。按下,它在最后一个元素后添加项目并增加长度。

var b = new Array(1);
b.push("hello world");
console.log(b.length); // print 2

vs

var v = [];
a.push("hello world");
console.log(b.length); // print 1

好吧,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():)

正如我所知道的区别,你可以找到切片(或数组的其他函数),如code1。和代码2显示u数组和他的实例:

code1:

[].slice; // find slice here
var arr = new Array();
arr.slice // find slice here
Array.prototype.slice // find slice here

code2:

[].__proto__ == Array.prototype; // true
var arr = new Array();
arr.__proto__ == Array.prototype; // true

结论:

如你所见[]和new Array()创建一个Array的新实例。它们都从array。prototype中获得原型函数

它们只是数组的不同实例。这就解释了为什么 [] != []

:)

在使用承诺时,我发现了一个不同。在使用承诺数组(例如arr,初始化为arr=[])时,在Promise.all(arr)中得到一个错误。而当声明为arr = Array()时,没有得到编译问题。希望这能有所帮助。