What is the difference between `new Object()` and object literal notation?

前端 未结 11 795
北恋
北恋 2020-11-22 07:41

What is the difference between this constructor-based syntax for creating an object:

person = new Object()

...and this literal syntax:

相关标签:
11条回答
  • 2020-11-22 08:26

    The only time i will use the 'new' keyowrd for object initialization is in inline arrow function:

    () => new Object({ key: value})
    

    since the below code is not valid:

    () => { key: value} //  instead of () => { return { key: value};}
    
    0 讨论(0)
  • 2020-11-22 08:27

    They both do the same thing (unless someone's done something unusual), other than that your second one creates an object and adds a property to it. But literal notation takes less space in the source code. It's clearly recognizable as to what is happening, so using new Object(), you are really just typing more and (in theory, if not optimized out by the JavaScript engine) doing an unnecessary function call.

    These

    person = new Object() /*You should put a semicolon here too.  
    It's not required, but it is good practice.*/ 
    -or-
    
    person = {
        property1 : "Hello"
    };
    

    technically do not do the same thing. The first just creates an object. The second creates one and assigns a property. For the first one to be the same you then need a second step to create and assign the property.

    The "something unusual" that someone could do would be to shadow or assign to the default Object global:

    // Don't do this
    Object = 23;
    

    In that highly-unusual case, new Object will fail but {} will work.

    In practice, there's never a reason to use new Object rather than {} (unless you've done that very unusual thing).

    0 讨论(0)
  • 2020-11-22 08:32

    On my machine using Node.js, I ran the following:

    console.log('Testing Array:');
    console.time('using[]');
    for(var i=0; i<200000000; i++){var arr = []};
    console.timeEnd('using[]');
    
    console.time('using new');
    for(var i=0; i<200000000; i++){var arr = new Array};
    console.timeEnd('using new');
    
    console.log('Testing Object:');
    
    console.time('using{}');
    for(var i=0; i<200000000; i++){var obj = {}};
    console.timeEnd('using{}');
    
    console.time('using new');
    for(var i=0; i<200000000; i++){var obj = new Object};
    console.timeEnd('using new');
    

    Note, this is an extension of what is found here: Why is arr = [] faster than arr = new Array?

    my output was the following:

    Testing Array:
    using[]: 1091ms
    using new: 2286ms
    Testing Object:
    using{}: 870ms
    using new: 5637ms
    

    so clearly {} and [] are faster than using new for creating empty objects/arrays.

    0 讨论(0)
  • 2020-11-22 08:35

    Also, according to some of the O'Really javascript books....(quoted)

    Another reason for using literals as opposed to the Object constructor is that there is no scope resolution. Because it’s possible that you have created a local constructor with the same name, the interpreter needs to look up the scope chain from the place you are calling Object() all the way up until it finds the global Object constructor.

    0 讨论(0)
  • 2020-11-22 08:38

    There is no difference for a simple object without methods as in your example. However, there is a big difference when you start adding methods to your object.

    Literal way:

    function Obj( prop ) { 
        return { 
            p : prop, 
            sayHello : function(){ alert(this.p); }, 
        }; 
    } 
    

    Prototype way:

    function Obj( prop ) { 
        this.p = prop; 
    } 
    Obj.prototype.sayHello = function(){alert(this.p);}; 
    

    Both ways allow creation of instances of Obj like this:

    var foo = new Obj( "hello" ); 
    

    However, with the literal way, you carry a copy of the sayHello method within each instance of your objects. Whereas, with the prototype way, the method is defined in the object prototype and shared between all object instances. If you have a lot of objects or a lot of methods, the literal way can lead to quite big memory waste.

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