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 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?
5 Answers 5
The advantages of object and array literals over using the respective constructors are:
- Shorter and more readable
- Safer: literals will still work when the
ArrayorObjectconstructors 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.
10 Comments
new Array() is because of the ambiguity when passing a single argument. The reasons you mentioned are valid but aren't major problemsFor 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).
3 Comments
new Array() method then added all the values with arr.push() - there were thousands of values to add (I had no life =_= ) and it was slow as... fudge. Just putting all the values insie the array constructor new Array(val1,val2,val3...) was hugely faster!.push(). The question here is more about constructor new Array(val1,val2,val3...) vs literal [val1,val2,val3...]new Object() and new Array() are a lot faster when you need to construct a ton of them and then set/append data very often. In a parser/decoder there's a noticeable speed gain when using them.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.
1 Comment
new Boolean(true) is worse than redundant, it's different to the primitive true. See my answer.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.
2 Comments
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
new Boolean(true)is not equivalent totrue