开发者

Why use {} instead of new Object() and use [] instead of new Array() and true/false instead of new Boolean()?

Many people say that you should avoid new Object, new Array()and instead use {}. [], and true/false.

What are the benefits of using the literal constructs to 开发者_JAVA百科get a new instance of an Object or Array rather than using new? I konw that Crockford doesn't like new but is that the main argument?


The advantages of object and array literals over using the respective constructors are:

  • Shorter and more readable
  • Safer: literals will still work when the Array or Object constructors have been overridden
  • Possibly faster, though it's unlikely to be a major consideration (any bottlenecks will almost certainly originate elsewhere in code)

In the case of arrays, there's an additional advantage of a literal: it's impossible to create an array with a single member using the Array constructor alone. For example, [3] will create an array with one element which is the number 3, while new Array(3) creates an array of length 3.

Update: the following paragraph is no longer relevant now the question has been edited.

Regarding Booleans, you seem to have a misconception: new Boolean(false) is not the same as false. The Boolean() constructor creates a Boolean object whereas false and true are Boolean primitives. In fact, new Boolean(false) evaluates to true when coerced into a Boolean in, for example, an if statement. In short, there's very rarely a reason to use the Boolean() constructor. Use true and false instead. Similarly, other primitives such as strings and numbers have corresponding String() and Number() constructors that produce String and Number objects that are different to primitive strings and numbers and should generally be avoided.


For example, if you want to do this:

{name:"bla",address:"address"}

the new Object() way would be:

var o = new Object();
o.name="bla";
o.address="address";

The first one is much shorter. And I think that it would be faster in many browsers, too (jsperf testcase).


I think it's mostly about succinctness.

Why write new Array() when you can write [], or new Object() when you can write {}?

Also new Boolean() is entirely redundant. A boolean is always going to need to be either true or false, so you should definitely use the built in constants for that.


In most cases an object literal or array literal is sufficient and easier to use. You can even call or apply prototype methods (e.g. [].prototype.slice.call(someObj)) It's mostly faster too. You'll may want to notice that {} and []‘s constructor cannot be overwritten and that Object and Array are constructors where {} and [] are instances.


One reason not yet mentioned is the ambiguity when passing parameters to the Array constructor. It's all specified behavior, it's just quirky.

new Array(1,2,3); // [1,2,3], that's OK
new Array(1); // [undefined], some may expect to get [1] instead

A JavaScript array is initialized with the given elements, except in the case where a single argument is passed to the Array constructor and that argument is a number. (See below.) Note that this special case only applies to JavaScript arrays created with the Array constructor, not array literals created with the bracket syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Syntax

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜