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

var myArray = new Array();

and

var myArray = [];

当前回答

第一个是默认的对象构造函数调用。主要用于动态值。

var array = new Array(length); //initialize with default length

第二个数组用于创建静态值

var array = [red, green, blue, yellow, white]; // this array will contain values.

其他回答

有关更多信息,下面的页面描述了为什么永远不需要使用new Array()

你永远不需要使用new Object()在 JavaScript。使用对象文字{} 代替。同样,不要使用new Array(), 使用数组文字[] 代替。JavaScript中的数组可以工作 不像Java中的数组,而且 使用类似java的语法 迷惑你。 不使用新数字,新字符串,或 新布尔。这些形式产生 不必要的对象包装。只使用 而是简单的字面量。

还要查看注释——新的Array(length)表单没有任何有用的用途(至少在今天的JavaScript实现中)。

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

我可以用更具体的方式来解释这个基于Fredrik的好例子的例子。

var test1 = [];
test1.push("value");
test1.push("value2");

var test2 = new Array();
test2.push("value");
test2.push("value2");

alert(test1);
alert(test2);
alert(test1 == test2);
alert(test1.value == test2.value);

我只是向数组中添加了另一个值,并发出了四个警告: 第一个和第二个是给我们存储在每个数组中的值,以确定这些值。他们会照样回来的! 再试试第三个,它返回false,这是因为

JS将test1视为具有数组数据类型的变量,将test2视为具有数组功能的对象,并且 这里有一些细微的区别。

第一个区别是,当我们调用test1时,它会不加思考地调用一个变量,它只返回存储在这个变量中的值,而不管它的数据类型! 但是,当我们调用test2时,它会调用Array()函数,然后将我们的“pushing”值存储在它的“Value”属性中,当我们提醒test2时也会发生同样的情况,它会返回数组对象的“Value”属性。

因此,当我们检查test1是否等于test2时,它们当然永远不会返回true,一个是函数,另一个是变量(具有数组类型),即使它们具有相同的值!

为了确保这一点,请尝试第4个警告,并将.value添加到它;它将返回true。在这种情况下,我们告诉JS“不管容器的类型是函数还是变量,请比较存储在每个容器中的值,并告诉我们你看到了什么!”这就是所发生的事情。

我希望我已经说得很清楚了,抱歉我的英语不好。

我在使用[]时出现了奇怪的行为。

我们有模型“类”,字段初始化为一些值。例如:

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)的问题

没有太大的区别,它们基本上做同样的事情,但以不同的方式来做,但是继续读下去,看看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),这也是不推荐的方式。