数组构造函数或文字 [英] Array constructor or literal

查看:81
本文介绍了数组构造函数或文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看Douglas Crockford的JSLint。


它期望的一个约定是使用
$ b创建数组$ b字面符号

var arr1 = [];


而不是使用构造函数

var arr2 = new Array( );


我一直在谷歌搜索,但我能找到的最好的原因是

因为它不是必须使用构造函数。


我想知道:


1)有没有办法区分arr1和arr2为

一旦定义了它们?


2)除了风格原因之外,还有什么方法可以选择一种方法而不是/>
另一个?

我对JSLint选择一个特定的约定和

应用它没有问题。我只是想知道是否有* *

以外的任何内容都支持文字而不是构造函数表示法。

解决方案

Dan Rumney< da ******* @ warpmail.net写道:


我一直在看看Douglas Crockford'的JSLint。


它期望的一个约定是使用

文字表示法创建数组

var arr1 = [] ;


而不是使用构造函数

var arr2 = new Array();


我''我一直在谷歌搜索,但我能找到的最好的原因是

因为它没有必要使用构造函数。


我想知道:


1)有没有办法告诉arr1和arr2之间的区别为

如上所述,一旦他们''已定义?



不,除非你覆盖默认的Array构造函数,否则你可能不应该这样做。


ES3与拟议ES4之间的兼容性文件讨论了

其中的一部分,见1.4节:

http://www.ecmascript.org/es4/spec/i...tibilities.pdf


2)除了风格原因之外,还有什么东西可以选择一种方法而不是另一种方法吗?



风格很重要:-)你更愿意看到什么:


var aoo = new Array(new Array() ,

新数组(1,2,新数组()));





var aoo = [[],

[1,2,[]]];


我绝对更喜欢第二种。数组文字从代码中删除了大量的混乱

。如果你看一下对象文字vs新的Object(),那么

的区别就更大了。


-

Joost Diepenmaat |博客: http://joost.zeekat.nl/ |工作: http://zeekat.nl/

6月12日下午2:48,Dan Rumney写道:


我一直在看Douglas Crockford的JSLint。 br />

它期望的一个约定是使用文字符号创建数组



var arr1 = [];


而不是使用构造函数

var arr2 = new Array();


我是一个-googling,但最好的理由是我可以找到
找到的是因为它没有必要使用构造函数。


我我想知道:


1)有没有办法告诉arr1和

arr2之间的区别,如上所定义的,一旦它们被定义了?



无。


2)除了风格原因之外,还有什么可以选择一个

方法超过另一个?



一个比另一个短,但两个都不清楚。


我没问题针对JSLint选择特定约定

并应用它。我只是想知道是否有任何超出*约定的
*代表

构造函数表示法。



一个比另一个短,但两个都不清楚。那可能是全部。



Dan Rumney< da ******* @ warpmail.netwrote:
< blockquote class =post_quotes>
我对选择特定约定的JSLint和

应用它没有问题。我只是想知道是否有任何*超出*惯例

,而不是构造符号表示支持文字。



我不知道这个特殊的惯例没有问题。我唯一可以认为
明确地使用Array构造函数的重要性

如果你有一个创建了一个对象的函数和<的类型br />
对象以某种方式变化。在这种情况下,虽然你会调用''new theType

()''而不是''new Array()''所以jslint无论如何也不会标记它,而且我是/>
想不到一个具体的例子,你会这样做。


我确实遇到了jslint生成的其他一些警告的问题

是我为什么会制作一个修改版本,让你忽略特定的错误

消息,如果你不喜欢它们。


我修改过的jslint位于:
http:// codespeak.net/svn/kupu/trunk/kupu/jslint.js 如果你有兴趣的话。

同样的版本适用于Rhino或微软的脚本引擎。


同样在同一个subversion文件夹中,lint.py是一个前端来运行jslint而不是
a很多javascript文件,每次他们都改变了(你可以'''使用make

因为没有o输出文件)和jslint.opts是我用于该特定项目的命令行

选项的集合。我压制了像

''标识符已经声明''和''不必要的分号''这样的东西,因为我发生了

更喜欢总是用''var'为我的循环变量添加前缀',而且我也更喜欢

无需回顾数百行代码来确定何时不需要分号。


-

Duncan Booth http:// kupuguy.blogspot.com


I''ve been taking a look at Douglas Crockford''s JSLint.

One of the conventions that it expects is that arrays be created using
literal notation
var arr1 = [];

as opposed to using a constructor
var arr2 = new Array();

I''ve been a-googling, but the best reason for this that I can find is
"because it''s not necessary to use a constructor".

I''m wondering:

1) Is there a way to tell the difference between arr1 and arr2 as
defined above, once they''ve been defined?

2) Is there anything, beyond stylstic reasons, to prefer one method over
another?
I''ve no problem against JSLint choosing a specific convention and
applying it. I''m just wondering if there is anything *beyond* convention
to speak in favour of literal over constructor notation.

解决方案

Dan Rumney <da*******@warpmail.netwrites:

I''ve been taking a look at Douglas Crockford''s JSLint.

One of the conventions that it expects is that arrays be created using
literal notation
var arr1 = [];

as opposed to using a constructor
var arr2 = new Array();

I''ve been a-googling, but the best reason for this that I can find is
"because it''s not necessary to use a constructor".

I''m wondering:

1) Is there a way to tell the difference between arr1 and arr2 as
defined above, once they''ve been defined?

No, unless you override the default Array constructor, which you
probably shouldn''t.

Tthe "Compatibility Between ES3 and Proposed ES4" document discusses
some of this, see section 1.4 of:

http://www.ecmascript.org/es4/spec/i...tibilities.pdf

2) Is there anything, beyond stylstic reasons, to prefer one method
over another?

Style is important :-) What would you rather see:

var aoo = new Array( new Array(),
new Array( 1, 2, new Array() ) );

Or

var aoo = [ [],
[ 1, 2, [] ] ];

I definitely prefer the second. Array literals remove a lot of clutter
from the code. And if you look at object literals vs new Object() the
difference is even bigger.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


On Jun 12, 2:48 pm, Dan Rumney wrote:

I''ve been taking a look at Douglas Crockford''s JSLint.

One of the conventions that it expects is that arrays be
created using literal notation
var arr1 = [];

as opposed to using a constructor
var arr2 = new Array();

I''ve been a-googling, but the best reason for this that I can
find is "because it''s not necessary to use a constructor".

I''m wondering:

1) Is there a way to tell the difference between arr1 and
arr2 as defined above, once they''ve been defined?

None.

2) Is there anything, beyond stylstic reasons, to prefer one
method over another?

One is shorter than the other, while neither are unclear.

I''ve no problem against JSLint choosing a specific convention
and applying it. I''m just wondering if there is anything
*beyond* convention to speak in favour of literal over
constructor notation.

One is shorter than the other, while neither are unclear. That is
probably all.


Dan Rumney <da*******@warpmail.netwrote:

I''ve no problem against JSLint choosing a specific convention and
applying it. I''m just wondering if there is anything *beyond* convention
to speak in favour of literal over constructor notation.

I don''t have a problem with this particular convention. The only time I can
think where it would be important to use the Array constructor explicitly
would be if you had a function which created an object and the type of the
object varied somehow. In that case though you''ld be calling ''new theType
()'' rather than ''new Array()'' so jslint wouldn''t flag it anyway, and I
can''t think of a concrete example where you''ld do that.

I do have issues with some of the other warnings generated by jslint which
is why I produced a modified version that lets you ignore particular error
messages if you don''t like them.

My modified jslint is at:
http://codespeak.net/svn/kupu/trunk/kupu/jslint.js if you are interested.
The same version works with either Rhino or Microsoft''s scripting engine.

Also in the same subversion folder lint.py is a frontend to run jslint over
a lot of javascript files, every time they''ve changed (you can''t use make
because there''s no output file) and jslint.opts is the set of command line
options I use for that particular project. I suppressed things like
''Indentifier already declared'' and ''Unnecessary semicolon'' because I happen
to prefer to always prefix my loop variables with ''var'', and I also prefer
to not have to look back through maybe hundreds of lines of code to
determine when a semi-colon is not required.

--
Duncan Booth http://kupuguy.blogspot.com


这篇关于数组构造函数或文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆