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

后端 未结 5 1103
南方客
南方客 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 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).

提交回复
热议问题