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

后端 未结 5 1091
南方客
南方客 2020-12-06 00:55

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

相关标签:
5条回答
  • 2020-12-06 01:43

    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.

    0 讨论(0)
  • 2020-12-06 01:46

    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.

    0 讨论(0)
  • 2020-12-06 01:47

    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 讨论(0)
  • 2020-12-06 02:00

    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).

    0 讨论(0)
  • 2020-12-06 02:00

    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.

    0 讨论(0)
提交回复
热议问题