new Array()vs [] [英] new Array() vs []
问题描述
你好,一个假装是/更好/使用新的Array()和* never *
使用文字符号[],是他错了还是对的?如果他错了,因为我怀疑他是,我怎么能用简单的条款解释他:)他错了?
http://javascript.crockford.com/code.html
此处在& ;奖金建议写道:
"使用{}而不是新的Object()。使用[]而不是新的Array()。
我已经看到它写了很多资源,但从来没有任何具体的
解释,为什么呢?
谢谢。
-
laurent
Hello, one is pretending it is /better/ to use new Array() and *never*
use the literal notation [], is he wrong or right ? If he is wrong, as i
suspect he is, how can i explain him, with simple terms :) he is wrong ?
http://javascript.crockford.com/code.html
Here in "Bonus Suggestions" it is write :
"Use {} instead of new Object(). Use [] instead of new Array()."
I''ve seen it write in a lot of ressources, but never with any concrete
explanation, so why ?
Thanks.
--
laurent
推荐答案
嗨Laurent。
Laurent vilday写道:
Hi Laurent.
Laurent vilday wrote:
您好,一个假装是/更好/使用新的Array()和* never *
使用文字符号[],他错了还是对的?如果他错了,因为我怀疑他是,我怎么能用简单的条款解释他:)他错了?
Hello, one is pretending it is /better/ to use new Array() and *never*
use the literal notation [], is he wrong or right ? If he is wrong, as i
suspect he is, how can i explain him, with simple terms :) he is wrong ?
更好的方式是什么?
ECMA-262说(11.1.4):
>
生产ArrayLiteral:[Elision(opt)]的计算方法如下:
1.创建一个新数组,就像使用表达式new Array()一样。 />
2.评估Elision;如果不存在,则使用数值零。
3.使用参数length调用Result(1)的[[Put]]方法。并且
结果(2)。
4.返回结果(1)。
从(15.4.2.1)你可以看到新的Array()会设置length。反正到0
。所以它们在行为方面也是一样的。
[]的类型更短,并且解析得更快(假设合理的实现是合理的) 。没有理由不使用它。
-
Cameron McCormack, http://mcc.id.au/
xmpp:他**** @ jabber.org a?a ICQ 26955922 a?a MSN ca*@mcc.id.au
Better in what way?
ECMA-262 says (11.1.4):
The production ArrayLiteral: [ Elision(opt) ] is evaluated as follows:
1. Create a new array as if by the expression new Array().
2. Evaluate Elision; if not present, use the numeric value zero.
3. Call the [[Put]] method of Result(1) with arguments "length" and
Result(2).
4. Return Result(1).
And from (15.4.2.1) you can see that new Array() would set "length" to 0
anyway. So they are just the same in terms of behaviour.
[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There''s no reason not to use it.
--
Cameron McCormack, http://mcc.id.au/
xmpp:he****@jabber.org a?a ICQ 26955922 a?a MSN ca*@mcc.id.au
Cameron McCormack写道:
Cameron McCormack wrote:
[]的类型更短,并且解析得更快(假设合理的实施是合理的)。没有理由不使用它。
[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There''s no reason not to use it.
古代浏览器并不支持它。
我甚至不记得哪个版本的Netscape - 3.0,或许?或者甚至
2.x?
不使用[]和{}可能是很久以前来过
结论的人的推荐在现代,它并没有改变它。
-
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Matt Kruse写道:
Matt Kruse wrote:
Cameron McCormack写道:
Cameron McCormack wrote:
[]的类型更短,并且解析得更快(假设
合理实施)。没有理由不使用它。
[] is shorter to type, and would be quicker to parse (assuming a
reasonable implementation). There''s no reason not to use it.
古代浏览器不支持它。
我甚至不记得哪个版本的Netscape - 3.0,也许?或者甚至
2.x?
不使用[]和{}可能是很久以前来过
结论的人的推荐在现代,它并没有改变它。
Ancient browsers didn''t support it.
I don''t even remember which versions of Netscape - 3.0, perhaps? Or even
2.x?
Not using [] and {} may be a recommendation from someone who came to the
conclusion long ago an just hasn''t changed it in modern times.
< URL: http:// jslint。 com >
INPUT
var x = new Array();
OUTPUT
第1行问题13:使用数组文字符号[]。
<URL: http://jslint.com >
INPUT
var x = new Array();
OUTPUT
Problem at line 1 character 13: Use the array literal notation [].
这篇关于new Array()vs []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!