When I need to declare a new array I use this notation
当我需要声明一个新的数组时,我使用这个符号
var arr = new Array();
But when testing online, for example on jsbin, a warning signals me to "Use the array literal notation []."
但是当在线测试时,例如在jsbin上,一个警告信号告诉我“使用数组文字符号[]”。
I didn't find a reason to avoid using the constructor. Is in some way less efficient than using []
? Or is it bad practice?
我找不到一个避免使用构造函数的理由。在某种程度上比使用[]效率低吗?还是不好的做法?
Is there a good reason to use var arr = [];
instead of var arr = new Array();
?
是否有充分的理由使用var arr = [];而不是var arr = new Array();?
9 个解决方案
#1
257
Mostly, people use var a = []
because Douglas Crockford says so.
大多数情况下,人们使用var a =[]是因为Douglas Crockford这么说。
His reasons include the non-intuitive and inconsistent behaviour of new Array()
:
他的原因包括新数组()的非直觉和不一致的行为:
var a = new Array(5); // an array pre-sized to 5 elements long
var b = new Array(5, 10); // an array with two elements in it
Note that there's no way with new Array()
to create an array with just one pre-specified number element in it!
注意,使用new Array()无法创建一个数组,其中只有一个预先指定的number元素!
Using []
is actually more efficient, and safer too! It's possible to overwrite the Array
constructor and make it do odd things, but you can't overwrite the behaviour of []
.
使用[]实际上更有效,也更安全!重写数组构造函数并使其执行奇怪的操作是可能的,但是不能重写[]的行为。
Personally, I always use the []
syntax, and similarly always use {}
syntax in place of new Object()
.
就我个人而言,我总是使用[]语法,同样也总是使用{}语法来代替new Object()。
#2
48
One significant difference is that []
will always instantiate a new Array, whereas new Array
could be hijacked to create a different object.
一个重要的区别是[]总是实例化一个新的数组,而新的数组可以被劫持来创建一个不同的对象。
(function () {
"use strict";
var foo,
bar;
//don't do this, it's a bad idea
function Array() {
alert('foo');
}
foo = new Array();
bar = [];
}());
In my example code, I've kept the Array
function hidden from the rest of the document scope, however it's more likely that if you ever run into this sort of issue that the code won't have been left in a nice closure, and will likely be difficult to locate.
在我的示例代码中,我将数组函数隐藏在文档范围的其余部分中,但是,如果您遇到这种问题,那么很可能代码不会被保存在一个漂亮的闭包中,而且很可能很难找到。
Disclaimer: It's not a good idea to hijack the Array
constructor.
免责声明:劫持数组构造函数不是一个好主意。
#3
26
for maintainability, use []
The array literal is more predictable, as most developers use it. Most array usage out there will be using the literal, and there is value in having your code match up with what other developers use.
与大多数开发人员使用的一样,数组文字更容易预测。大多数数组的使用都会使用文字,让代码与其他开发人员使用的代码匹配是有价值的。
for empty arrays, use []
var ns = [];
var names = [ 'john', 'brian' ];
As shown here, using the literal for empty and a couple of known elements is fatster than the Array constructor.
如图所示,使用文字表示为empty和一些已知元素比数组构造函数更胖。
for an array of known size, use new Array(size)
If the size is known, then using the Array constructor significantly improves performance. However it also means you have to deal with an array which already has 'undefined' filling all it's values.
如果知道大小,那么使用数组构造函数可以显著提高性能。然而,它也意味着你必须处理一个数组,它已经没有定义填充所有的值。
As shown here
如下所示
// fill an array with 100 numbers
var ns = new Array( 100 );
for ( var i = 0; i < 100; i++ ) {
ns[i] = i;
}
This also works for very small arrays too.
这也适用于非常小的数组。
#4
12
No, there is actually no reason to use one notation over the other one for empty Arrays.
不,实际上没有理由在空数组中使用一个符号。
However, most browsers show a slightly better performance using x = [];
than calling the Constructor.
然而,大多数浏览器在使用x =[]时表现出稍好的性能;比调用构造函数。
If you need to create an Array with a specific size, you kind of need to use x = new Array(10);
for instance, which would create an Array with 10 undefined
slots.
如果您需要创建具有特定大小的数组,您需要使用x = new Array(10);例如,它将创建一个具有10个未定义槽的数组。
#5
6
- var arr=[] uses the array/object literal
- var arr=[]使用数组/对象文本
- var arr = new Array() use the array/object constructor
- var arr = new Array()使用数组/对象构造函数
The speediest way to define an array or object is literal way, because you don't need to call the constructor
定义数组或对象最快速的方法是字面方式,因为您不需要调用构造函数
var arr1 = new Array(1, 2, 3, 4);
var arr2 = [1, 2, 3, 4];
alert(arr1[0]); // 1
alert(arr2[0]); // 1
var arr3 = new Array(200);
var arr4 = [200];
alert(arr3[0]); // 'undefined'
alert(arr4[0]); // 200
#6
6
Both are correct only. But most of the people use var a = []
两者都是正确的。但是大多数人使用var a = []
Three Ways to Declare an Array in JavaScript.
用JavaScript声明数组的三种方法。
method 1: We can explicitly declare an array with the JavaScript "new" keyword to instantiate the array in memory (i.e. create it, and make it available).
方法1:我们可以使用JavaScript“new”关键字显式地声明一个数组来实例化内存中的数组(例如,创建数组并使其可用)。
// Declare an array (using the array constructor)
var arlene1 = new Array();
var arlene2 = new Array("First element", "Second", "Last");
method 2: we use an alternate method to declaring arrays.
方法2:我们使用另一种方法来声明数组。
// Declare an array (using literal notation)
var arlene1 = [];
var arlene2 = ["First element", "Second", "Last"];
method 3: JavaScript also lets you create arrays indirectly, by calling specific methods.
方法3:JavaScript还允许通过调用特定的方法间接创建数组。
// Create an array from a method's return value
var carter = "I-learn-JavaScript";
var arlene3 = carter.split("-");
#7
4
There is a limit the constructor can take as arguments.
构造函数可以用作参数。
On most systems I encoutered the limit is 2^16 (2 bytes):
在大多数系统我encoutered极限是2 ^ 16(2字节):
var myFreshArr = new Array(0,1,2,3 ..... 65536);
That will cause an error (too many arguments....)
这将导致一个错误(太多的参数....)
When using the literal [] you don't have such problems.
当使用文字[]时,你不会有这样的问题。
In case you don't care about such big arrays, I think you can use whatever you prefer.
如果您不关心这么大的数组,我认为您可以使用任何您喜欢的。
#8
4
var array = [ 1, 2, 3, 4];
is sugar
是糖
var array = new Array(1, 2, 3, 4);
is salt
是盐
#9
3
It's because new Array()
is ambiguous. These are the correct constructors:
因为new Array()是不明确的。这些是正确的建设者:
// Using brackets
[element0, element1, ..., elementN]
// Using new AND a list of elements
new Array(element0, element1, ..., elementN)
// Using new AND an integer specifying the array length
new Array(arrayLength)
#1
257
Mostly, people use var a = []
because Douglas Crockford says so.
大多数情况下,人们使用var a =[]是因为Douglas Crockford这么说。
His reasons include the non-intuitive and inconsistent behaviour of new Array()
:
他的原因包括新数组()的非直觉和不一致的行为:
var a = new Array(5); // an array pre-sized to 5 elements long
var b = new Array(5, 10); // an array with two elements in it
Note that there's no way with new Array()
to create an array with just one pre-specified number element in it!
注意,使用new Array()无法创建一个数组,其中只有一个预先指定的number元素!
Using []
is actually more efficient, and safer too! It's possible to overwrite the Array
constructor and make it do odd things, but you can't overwrite the behaviour of []
.
使用[]实际上更有效,也更安全!重写数组构造函数并使其执行奇怪的操作是可能的,但是不能重写[]的行为。
Personally, I always use the []
syntax, and similarly always use {}
syntax in place of new Object()
.
就我个人而言,我总是使用[]语法,同样也总是使用{}语法来代替new Object()。
#2
48
One significant difference is that []
will always instantiate a new Array, whereas new Array
could be hijacked to create a different object.
一个重要的区别是[]总是实例化一个新的数组,而新的数组可以被劫持来创建一个不同的对象。
(function () {
"use strict";
var foo,
bar;
//don't do this, it's a bad idea
function Array() {
alert('foo');
}
foo = new Array();
bar = [];
}());
In my example code, I've kept the Array
function hidden from the rest of the document scope, however it's more likely that if you ever run into this sort of issue that the code won't have been left in a nice closure, and will likely be difficult to locate.
在我的示例代码中,我将数组函数隐藏在文档范围的其余部分中,但是,如果您遇到这种问题,那么很可能代码不会被保存在一个漂亮的闭包中,而且很可能很难找到。
Disclaimer: It's not a good idea to hijack the Array
constructor.
免责声明:劫持数组构造函数不是一个好主意。
#3
26
for maintainability, use []
The array literal is more predictable, as most developers use it. Most array usage out there will be using the literal, and there is value in having your code match up with what other developers use.
与大多数开发人员使用的一样,数组文字更容易预测。大多数数组的使用都会使用文字,让代码与其他开发人员使用的代码匹配是有价值的。
for empty arrays, use []
var ns = [];
var names = [ 'john', 'brian' ];
As shown here, using the literal for empty and a couple of known elements is fatster than the Array constructor.
如图所示,使用文字表示为empty和一些已知元素比数组构造函数更胖。
for an array of known size, use new Array(size)
If the size is known, then using the Array constructor significantly improves performance. However it also means you have to deal with an array which already has 'undefined' filling all it's values.
如果知道大小,那么使用数组构造函数可以显著提高性能。然而,它也意味着你必须处理一个数组,它已经没有定义填充所有的值。
As shown here
如下所示
// fill an array with 100 numbers
var ns = new Array( 100 );
for ( var i = 0; i < 100; i++ ) {
ns[i] = i;
}
This also works for very small arrays too.
这也适用于非常小的数组。
#4
12
No, there is actually no reason to use one notation over the other one for empty Arrays.
不,实际上没有理由在空数组中使用一个符号。
However, most browsers show a slightly better performance using x = [];
than calling the Constructor.
然而,大多数浏览器在使用x =[]时表现出稍好的性能;比调用构造函数。
If you need to create an Array with a specific size, you kind of need to use x = new Array(10);
for instance, which would create an Array with 10 undefined
slots.
如果您需要创建具有特定大小的数组,您需要使用x = new Array(10);例如,它将创建一个具有10个未定义槽的数组。
#5
6
- var arr=[] uses the array/object literal
- var arr=[]使用数组/对象文本
- var arr = new Array() use the array/object constructor
- var arr = new Array()使用数组/对象构造函数
The speediest way to define an array or object is literal way, because you don't need to call the constructor
定义数组或对象最快速的方法是字面方式,因为您不需要调用构造函数
var arr1 = new Array(1, 2, 3, 4);
var arr2 = [1, 2, 3, 4];
alert(arr1[0]); // 1
alert(arr2[0]); // 1
var arr3 = new Array(200);
var arr4 = [200];
alert(arr3[0]); // 'undefined'
alert(arr4[0]); // 200
#6
6
Both are correct only. But most of the people use var a = []
两者都是正确的。但是大多数人使用var a = []
Three Ways to Declare an Array in JavaScript.
用JavaScript声明数组的三种方法。
method 1: We can explicitly declare an array with the JavaScript "new" keyword to instantiate the array in memory (i.e. create it, and make it available).
方法1:我们可以使用JavaScript“new”关键字显式地声明一个数组来实例化内存中的数组(例如,创建数组并使其可用)。
// Declare an array (using the array constructor)
var arlene1 = new Array();
var arlene2 = new Array("First element", "Second", "Last");
method 2: we use an alternate method to declaring arrays.
方法2:我们使用另一种方法来声明数组。
// Declare an array (using literal notation)
var arlene1 = [];
var arlene2 = ["First element", "Second", "Last"];
method 3: JavaScript also lets you create arrays indirectly, by calling specific methods.
方法3:JavaScript还允许通过调用特定的方法间接创建数组。
// Create an array from a method's return value
var carter = "I-learn-JavaScript";
var arlene3 = carter.split("-");
#7
4
There is a limit the constructor can take as arguments.
构造函数可以用作参数。
On most systems I encoutered the limit is 2^16 (2 bytes):
在大多数系统我encoutered极限是2 ^ 16(2字节):
var myFreshArr = new Array(0,1,2,3 ..... 65536);
That will cause an error (too many arguments....)
这将导致一个错误(太多的参数....)
When using the literal [] you don't have such problems.
当使用文字[]时,你不会有这样的问题。
In case you don't care about such big arrays, I think you can use whatever you prefer.
如果您不关心这么大的数组,我认为您可以使用任何您喜欢的。
#8
4
var array = [ 1, 2, 3, 4];
is sugar
是糖
var array = new Array(1, 2, 3, 4);
is salt
是盐
#9
3
It's because new Array()
is ambiguous. These are the correct constructors:
因为new Array()是不明确的。这些是正确的建设者:
// Using brackets
[element0, element1, ..., elementN]
// Using new AND a list of elements
new Array(element0, element1, ..., elementN)
// Using new AND an integer specifying the array length
new Array(arrayLength)