像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
当前回答
正如我所知道的区别,你可以找到切片(或数组的其他函数),如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中获得原型函数
它们只是数组的不同实例。这就解释了为什么 [] != []
:)
其他回答
没有太大的区别,它们基本上做同样的事情,但以不同的方式来做,但是继续读下去,看看W3C的这个声明:
var cars = ["Saab", "Volvo","BMW"];
and
var cars = new Array("Saab", "Volvo", "BMW");
上面的两个例子完全相同。没有使用的必要 新数组()。为简化性、可读性和执行速度,请使用 第一个(数组字面量方法)。
但同时,使用新的array语法创建新数组被认为是一种糟糕的做法:
避免使用新数组() 不需要使用JavaScript内置的数组构造函数 新数组()。 请使用[]。 这两个不同的语句都创建了一个名为 点:
var points = new Array(); // Bad
var points = []; // Good
这两个不同的语句都创建了一个包含6的新数组 数字:
var points = new Array(40, 100, 1, 5, 25, 10); // Bad
var points = [40, 100, 1, 5, 25, 10]; // Good
new关键字只会使代码复杂化。它也能产生一些 意想不到的结果:
var points = new Array(40, 100); // Creates an array with two elements (40 and 100)
如果我去掉其中一个元素呢?
var points = new Array(40); // Creates an array with 40 undefined elements !!!!!
所以基本上不被认为是最佳实践,这里还有一个小区别,你可以像这样传递length到new Array(length),这也是不推荐的方式。
有区别,但在那个例子中没有区别。
使用更详细的方法: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会使依赖数组变得困难。用于计算的长度。
有一个重要的区别,目前还没有答案提到。
从这个:
new Array(2).length // 2
new Array(2)[0] === undefined // true
new Array(2)[1] === undefined // true
你可能认为新的Array(2)等同于[undefined, undefined],但事实并非如此!
让我们尝试使用map():
[undefined, undefined].map(e => 1) // [1, 1]
new Array(2).map(e => 1) // "(2) [undefined × 2]" in Chrome
看到了吗?语义完全不同!为什么呢?
根据ES6 Spec 22.1.1.2, Array(len)的任务只是创建一个新数组,其属性长度设置为参数len,这就意味着在这个新创建的数组中没有任何实际元素。
函数map(),根据规范22.1.3.15将首先检查HasProperty然后调用回调,但结果是:
new Array(2).hasOwnProperty(0) // false
[undefined, undefined].hasOwnProperty(0) // true
这就是为什么你不能期望任何迭代函数像往常一样在new Array(len)创建的数组上工作。
顺便说一句,Safari和Firefox在这种情况下有更好的“打印”:
// Safari
new Array(2) // [](2)
new Array(2).map(e => 1) // [](2)
[undefined, undefined] // [undefined, undefined] (2)
// Firefox
new Array(2) // Array [ <2 empty slots> ]
new Array(2).map(e => 1) // Array [ <2 empty slots> ]
[undefined, undefined] // Array [ undefined, undefined ]
我已经向Chromium提交了一个问题,并要求他们修复这个令人困惑的打印: https://bugs.chromium.org/p/chromium/issues/detail?id=732021
更新:已经修复了。Chrome现在打印为:
new Array(2) // (2) [empty × 2]
好吧,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():)
我发现了这两种结构之间的一个区别,这让我很难受。
假设我有:
function MyClass(){
this.property1=[];
this.property2=new Array();
};
var MyObject1=new MyClass();
var MyObject2=new MyClass();
在现实生活中,如果我这样做:
MyObject1.property1.push('a');
MyObject1.property2.push('b');
MyObject2.property1.push('c');
MyObject2.property2.push('d');
我最后得出的结论是:
MyObject1.property1=['a','c']
MyObject1.property2=['b']
MyObject2.property1=['a','c']
MyObject2.property2=['d']
我不知道语言规范说应该发生什么,但如果我想让我的两个对象在我的对象中有唯一的属性数组,我必须使用new Array()。