Home  >  Q&A  >  body text

How to create objects in JavaScript

The methods for creating objects in Javascript include object literals, new constructors, Object.create(), etc. Is the Object.create() method the best way to embody the prototype-based thinking of Javascript? How does the object created by the new constructor reflect the prototype-based idea? Which of the two methods, object literal method or new constructor, appeared earlier?

高洛峰高洛峰2686 days ago756

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:31:35

    Sorry, I don’t know much about the first question. I will add more after I research it.

    Second question:
    When we create a constructor, there is a Prototype attribute inside it. This attribute is a pointer pointing to the prototype object of the constructor
    It is instantiated through the new operator + constructor Object has an internal __proto__ attribute, which also points to the prototype object

    function Foo(name){
        this.name = name;
    }
    
    Foo.prototype.getName = function(){
        return this.name;
    };
    
    var obj = new Foo("suoz");
    alert(obj.getName());

    I think what can be reflected is the mechanism of searching variables through the prototype chain

    When searching for a variable, it will be searched in the instance attributes. If it cannot be found, it will be searched in the prototype object (the object pointed to by the object's __proto__), all the way to Object.prototype (because every function in JavaScript is Object), returns if found, otherwise returns undefined

    The third question:
    The new+ constructor must have appeared first, because object literals were introduced by later developers to simplify operations. Furthermore, var obj = {}; is actually executed. In fact, the actual execution code inside the system is like this var obj = new Object();

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-12 09:31:35

    First question:
    There is an explanation about Object.create() on MDN, you can take a look
    https://developer.mozilla.org... (If you have difficulty in English, you can switch to Chinese to browse)
    Second question:
    new is a JS keyword. We can guess the underlying mechanism of this language, but we dare not say what it looks like. How does it reflect the prototype? This question is a bit weird. I think the prototype of JS objects is not reflected by new, but is determined by the prototype chain mechanism of the object in this language. Then the phenomenon that emerges is the upward search mechanism (what the person above said Search mechanism)
    The third question:
    When using literals to create objects,
    first create an empty object {};
    Let the __proto__ of the empty object point to Object.prototype;
    Bind this to this Above the empty object;
    Bind the properties and methods in the created object to this;
    Finally return this object;
    This is basically what it looks like. If there are any errors, please point them out, thank you! (So ​​that my mistakes will not mislead others).

    reply
    0
  • Cancelreply