Constructor
Constructor returns a reference to the constructor when the instance object was created. The value of this property is a reference to the function itself, not a string containing the function name.
function Parent(){ this.age = age } var p = new Parent(50) p.constructor === Parent // true p.constructor === Object // false
The constructor itself is a function, which is no different from ordinary functions, but its initial letter is generally capitalized for specification. The difference between a constructor and an ordinary function is that the function that uses new to generate an instance is a constructor, and the ordinary function is called directly.
Is symbol a constructor
Symbol is a basic data type, but it is not complete as a constructor because it does not support the syntax new Symbol(). Chrome does not think it is a constructor. If you want to generate an instance, you can directly use Symbol().
new Symbol(123); // Symbol is not a constructor Symbol(123); // Symbol(123)
Although it is a basic data type, the Symbol(123) instance can obtain the constructor attribute value.
var sym = Symbol(123); console.log( sym ); // Symbol(123) console.log( sym.constructor ); // ƒ Symbol() { [native code] }
The constructor attribute here is actually on the Symbol prototype, that is, Symbol.prototype.constructor returns the function to create the instance prototype. The default is Symbol function.
Write a new
function create() { // Create an empty object var obj = new Object() // Get the constructor, and get the first parameter in arguments Con = [].shift.call(arguments) // Linked to the prototype, obj can access the properties in the constructor prototype obj.__proto__ = Con.prototype // Bind this to implement inheritance, and obj can access the properties in the constructor var ret = Con.apply(obj, arguments) // Returns the object returned by the constructor first return ret instanceof Object ? ret : obj }
prototype
Each object has a prototype object. The object takes its prototype as the template and inherits methods and properties from the prototype. These properties and methods are defined on the prototype property of the object constructor function, not the object instance itself.
Prototype chain
Each object has a prototype object through__ proto__ The pointer points to the previous prototype and inherits methods and properties from it. At the same time, the prototype object may also have a prototype, layer by layer, and finally point to null. This relationship is called prototype chain. Through prototype chain, an object will have properties and methods defined in other objects.
Let's look at the following example.
function Parent(age) { this.age = age; } var p = new Parent(50); p.constructor === Parent; // true
Here, p.constructor points to Parent. Does that mean that there is a constructor attribute in the P instance? Not at all.
Let's print out the value of P.
As can be seen from the figure, the instance object p itself has no constructor attribute, and is searched upward through the prototype chain__ proto__, Finally, the constructor attribute is found, which points to the constructor Parent.
function Parent(age) { this.age = age; } var p = new Parent(50); p; // Parent {age: 50} p.__proto__ === Parent.prototype; // true p.__proto__.__proto__ === Object.prototype; // true p.__proto__.__proto__.__proto__ === null; // true
The following figure shows the operation mechanism of the prototype chain.
Summary
- Symbol is not complete as a constructor, because the syntax new Symbol() is not supported, but its prototype has the constructor attribute, namely Symbol.prototype.constructor.
- The reference type constructor attribute value can be modified, but it is read-only for basic types. Of course, null and undefined have no constructor attribute.
- __ proto__ Is an attribute on each instance. Prototype is an attribute of the constructor. It does not exist on the instance, so the two are different. But P__ Prototype and Parent.prototype point to the same object.
- __ proto__ Property is standardized in es6, but it is not recommended because of performance problems. Object.getPrototypeOf() is recommended.
- Each object has a prototype object through__ proto__ The pointer points to the previous prototype and inherits methods and properties from it. At the same time, the prototype object may also have a prototype, layer by layer, and finally points to null. This is the prototype chain.