Home  >  Q&A  >  body text

javascript - Isn't this an object literal function? Why do you need new initialization?

I remember this is not a constructor. Why are this and new used the same as constructors?

淡淡烟草味淡淡烟草味2663 days ago842

reply all(4)I'll reply

  • 学习ing

    学习ing2017-07-05 10:54:40

    Any function in javascript can be called a constructor as long as it is called with the new keyword.

    When using the new keyword to call a function, an object will be implicitly declared inside the function, and then the object will be assigned to this, and finally this will be returned implicitly. When using the new keyword to call the author's book method, it is equivalent to The following process.

    function Book(id, bookname){
        var o = new Object();
        o.id = id;
        o.bookname = bookname;
        return o;
    }
    var b = Book(123,'javascript高级程序设计');

    The o here is actually the this object we usually see.

    reply
    0
  • 高洛峰

    高洛峰2017-07-05 10:54:40

    There are no classes or constructors in JavaScript.

    function Book (){}
    
    // Book 是一个函数
    typeof Book
    "function"
    
    // Book 是 Function
    Book instanceof Function
    true
    
    // Book 是 Object
    Book instanceof Object
    true
    

    Use new to create objects

    var book = new Book()
    
    // book 是一个对象
    typeof book
    "object"
    
    // book 不是 Function 的实例
    book instanceof Function
    false
    
    // book 是 Object 的实例
    book instanceof Object
    true
    
    // book 是 Book 的实例
    book instanceof Book
    true
    

    Although there is no constructor in js, this word is also used in the MDN documentation: https://developer.mozilla.org...

    When the code new foo(...) is executed:

    • A new object is created. It inherits from foo.prototype.

    • Constructor foo is executed. When executing, the corresponding parameters will be passed in, and the context (this) will be designated as this new instance. new foo is equivalent to new foo(), and can only be used without passing any parameters.

    • If the constructor returns an "object", then this object will replace the entire new result. If the constructor does not return an object, then the result of new is the object created in step 1. ps: Generally, the constructor does not return any value, but if the user wants to override this return value, he or she can choose to return an ordinary object. cover. Of course, returning an array will also be overwritten, because arrays are also objects.

    Even if ES6 adds classes, they are just syntactic sugar:

    class Book{}
    
    typeof Book
    "function"
    

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-07-05 10:54:40

    You misunderstood the concept of constructor.

    Actually, there should not be the term ‘constructor’ in js, but should be understood as the construction method of a function. This means that any function can be called new, and any function can be called a ‘constructor’. When you wrote the so-called "constructor", did you notice any difference between it and ordinary functions? No, they are just ordinary functions.

    Above, except es6 arrow function.

    reply
    0
  • 習慣沉默

    習慣沉默2017-07-05 10:54:40

    Every function in js is equivalent to a constructor (except for the true ES6 arrow function).
    Hey, amazing js!

    reply
    0
  • Cancelreply