In JSLint, it warns that
在JSLint,它警告说。
var x = new Array();
(That's not a real variable name) should be
(这不是一个真正的变量名)应该是。
var result = [];
What is wrong with the 1st syntax? What's the reasoning behind the suggestion?
第一个语法有什么问题?这个建议的理由是什么?
5 个解决方案
#1
26
Crockford doesn't like new
. Therefore, JSLint expects you to avoid it when possible. And creating a new array object is possible without using new
....
Crockford不喜欢新的。因此,JSLint希望您尽可能避免使用它。创建一个新的数组对象是不可能的
#2
69
It's safer to use []
than it is to use new Array()
, because you can actually override the value of Array
in JavaScript:
使用[]比使用new Array()更安全,因为您实际上可以在JavaScript中重写数组的值:
Array = function() { };
var x = new Array();
// x is now an Object instead of an Array.
In other words, []
is unambiguous.
换句话说,[]是明确的。
#3
7
It seems like you can get different performance based on which you are using and for what purpose depending on browser or environment:
似乎你可以根据你的使用情况和基于浏览器或环境的目的获得不同的性能:
http://jsperf.com/new-array-vs-literal/11 ( [1,.2] vs new Array(1,.2) ) the literal is way faster in this circumstance.
http://jsperf.com/new-array-vs-literal/11([1,。2)vs新数组(1,。2))在这种情况下,文字的速度更快。
http://jsperf.com/new-array-vs-literal/7 ( new Array(500000) vs [].length(500000) ) new Array is faster in chrome v21 it seems for this test by about 7% or 30%) depending on what you do.
new Array(500000) vs .length(500000))新数组在chrome v21中速度更快,这取决于你做什么。
#4
4
Nothing wrong with either form, but you usually see literals used wherever possible-
这两种形式都没有错,但你通常会在任何可能的地方看到文字。
var s='' is not more correct than var s=new String()....
var = "不正确比var年代=新的字符串()....
#5
2
There's nothing wrong with the first syntax per se. In fact, on w3schools, it lists new Array()
as the way to create an array. The problem is that this is the "old way." The "new way", []
is shorter, and allows you to initialize values in the array, as in ["foo", "bar"]
. Most developers prefer []
to new Array()
in terms of good style.
第一个语法本身没有什么问题。事实上,在w3schools中,它列出了新数组()作为创建数组的方法。问题是,这是“老办法”。“新方法”,[]较短,允许您在数组中初始化值,如在["foo", "bar"]中。大多数开发人员都喜欢用良好的样式来进行新数组()。
#1
26
Crockford doesn't like new
. Therefore, JSLint expects you to avoid it when possible. And creating a new array object is possible without using new
....
Crockford不喜欢新的。因此,JSLint希望您尽可能避免使用它。创建一个新的数组对象是不可能的
#2
69
It's safer to use []
than it is to use new Array()
, because you can actually override the value of Array
in JavaScript:
使用[]比使用new Array()更安全,因为您实际上可以在JavaScript中重写数组的值:
Array = function() { };
var x = new Array();
// x is now an Object instead of an Array.
In other words, []
is unambiguous.
换句话说,[]是明确的。
#3
7
It seems like you can get different performance based on which you are using and for what purpose depending on browser or environment:
似乎你可以根据你的使用情况和基于浏览器或环境的目的获得不同的性能:
http://jsperf.com/new-array-vs-literal/11 ( [1,.2] vs new Array(1,.2) ) the literal is way faster in this circumstance.
http://jsperf.com/new-array-vs-literal/11([1,。2)vs新数组(1,。2))在这种情况下,文字的速度更快。
http://jsperf.com/new-array-vs-literal/7 ( new Array(500000) vs [].length(500000) ) new Array is faster in chrome v21 it seems for this test by about 7% or 30%) depending on what you do.
new Array(500000) vs .length(500000))新数组在chrome v21中速度更快,这取决于你做什么。
#4
4
Nothing wrong with either form, but you usually see literals used wherever possible-
这两种形式都没有错,但你通常会在任何可能的地方看到文字。
var s='' is not more correct than var s=new String()....
var = "不正确比var年代=新的字符串()....
#5
2
There's nothing wrong with the first syntax per se. In fact, on w3schools, it lists new Array()
as the way to create an array. The problem is that this is the "old way." The "new way", []
is shorter, and allows you to initialize values in the array, as in ["foo", "bar"]
. Most developers prefer []
to new Array()
in terms of good style.
第一个语法本身没有什么问题。事实上,在w3schools中,它列出了新数组()作为创建数组的方法。问题是,这是“老办法”。“新方法”,[]较短,允许您在数组中初始化值,如在["foo", "bar"]中。大多数开发人员都喜欢用良好的样式来进行新数组()。