1. What is the constructor
A constructor is an ordinary function with the new operator in front of it. It is also a function, so constructors have the prototype property of the function.
2. What is an instance
An instance is an object created through a constructor.
var A = function () {
this.name = Naruto whirlpool
}
var obj = new A()
Copy the code
3. What is the prototype
A prototype is a prototype object, whose prototype object depends on the function’s prototype attribute and the instance’s __proto__ attribute.
4. What is the prototype chain
The prototype chain refers to an instance object whose __proto__ attribute points to its prototype object. For example, if obj is used to represent instances, the prototype object is represented as obj._proto_. At the same time, the prototype object is an object. And then there’s the next-level prototype object, which is also an instance object relative to the next-level prototype object, which also has a __proto__ property, which points to its protoobject, and so on, Object. Prototype is the end point of the prototype chain.
As you can see from the first property in the red box above, obj has reached Object by calling the __proto__ property twice. Object is a constructor. Object has the property Prototype, which points to its prototype. The third call returns null, indicating that Object.prototype is the end of the prototype chain.
5. The relationship between constructors and instances
Instances are created through constructors
The relationship between constructors and archetypes (objects)
The constructor uses its prototype property to find its associated prototype. If M is used for the constructor, a.protoType refers to its associated prototype object. The prototype object can use the constructor to find its associated constructor. A.prototype.conctructor===A.
Properties and methods on prototype objects can be shared by instances
var A = function (name) {
this.name = name
}
// Define properties and methods on the constructor prototype object
A.prototype.country = "AS"
A.prototype.say = function () {
console.log('Hey hey hey');
}
var obj1 = new A(Naruto whirlpool)
var obj2 = new A('Uchiba Sasuke')
Copy the code
Each call returns true, indicating that properties and methods on the prototype object can be shared by the instance.