像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
像这样声明一个数组的真正区别是什么:
var myArray = new Array();
and
var myArray = [];
当前回答
使用Array构造函数创建一个所需长度的新数组,并将每个下标填充为undefined,将赋值给变量one的数组创建您为其提供信息的下标。
其他回答
好吧,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():)
使用隐式数组创建数组与使用数组构造函数创建数组之间的区别很微妙,但很重要。
创建数组时使用
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 Object()在 JavaScript。使用对象文字{} 代替。同样,不要使用new Array(), 使用数组文字[] 代替。JavaScript中的数组可以工作 不像Java中的数组,而且 使用类似java的语法 迷惑你。 不使用新数字,新字符串,或 新布尔。这些形式产生 不必要的对象包装。只使用 而是简单的字面量。
还要查看注释——新的Array(length)表单没有任何有用的用途(至少在今天的JavaScript实现中)。
我发现了这两种结构之间的一个区别,这让我很难受。
假设我有:
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()。
我在使用[]时出现了奇怪的行为。
我们有模型“类”,字段初始化为一些值。例如:
require([
"dojo/_base/declare",
"dijit/_WidgetBase",
], function(declare, parser, ready, _WidgetBase){
declare("MyWidget", [_WidgetBase], {
field1: [],
field2: "",
function1: function(),
function2: function()
});
});
我发现当字段用[]初始化时,它将被所有Model对象共享。对其中一项进行更改会影响所有其他项。
用new Array()初始化它们时不会发生这种情况。对象的初始化也是一样({}vs new Object())
TBH我不确定这是否是我们使用的框架(Dojo)的问题