How to create objects in JavaScript
高洛峰
高洛峰 2017-06-12 09:29:35
0
2
731

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?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (2)
Ty80

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 theprototype 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 thisvar obj = new Object();

    女神的闺蜜爱上我

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

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!