Prototype chain is a way to implement inheritance, extending the prototype search mechanism, so that the search can be inherited up, search the prototype of the prototype.
Prototype search mechanism
In the prototype search mechanism, the relationship between the constructor, the instance, and the prototype object is as follows: the prototype object is a property of the constructor; the prototype object has a property constructor that refers back to the constructor; the constructor generates an instance that has a pointer to the prototype object on the constructor.
Code implementation
// constructor
function person(name, color){
this.name = name;
this.color = color;
}
// Add values to the default stereotype object
person.prototype.getColor = function(){
return this.color;
}
The instance contains a pointer to the prototype object
var personOne = new person("tom"."red");
console.log("Attribute from constructor", personOne.name) // "tom"
console.log("Attributes from the prototype object", personOne.getColor()) // "red"
Copy the code
Structure diagram
The relationship between constructors, prototype objects, and instances is as follows.
Search mechanism
When searching for an attribute/method, the instance itself is searched first, and if the instance itself is not found, the prototype object to which it points is found from the pointer, looking up properties similar to a scope chain.
That is, if the attribute name to look up is set in the instance, it will not look up again.
. . personOne.getColor =function(){
return "Properties in instance";
}
console.log(personOne.getColor()); // "Properties in instance". .Copy the code
If no pointer is found and there is also a pointer to another prototype object in the prototype object, the prototype is searched from the pointer up until the end of the prototype chain, that is, the prototype no longer contains a pointer to another prototype.
Prototype chain code implementation
From this we can see that the prototype chain is A chain of multiple prototype objects connected by A pointer to the prototype object. It states that when we new an instance of A, the instance contains A pointer to the prototype object.
If we assign this instance to the prototype object of another object instance B, the prototype chain is formed, and the search order is.
Code implementation
In the following code, when objB’s instance object obj is accessed, the values in objA’s prototype object are finally printed through the prototype chain.
function objA(){
this.title = 'A';
}
objA.prototype.name = "objA-prototype";
function objB(){
this.title = 'B';
}
objB.prototype = new objA();
var obj = new objB;
console.log(obj.name); // objA-prototype
Copy the code
Structure diagram
The chain development
We can extend the prototype chain by replacing the prototype object of the C object instance with an instance of B. After expansion, the search order is.
This is how inheritance is implemented in a prototype chain.
In the above formula, in the process of C instance searching for prototype A, as long as it obtains an attribute of the same name in A certain place, it will take the attribute instead of searching up, that is to say, adding the existing method of the upper prototype chain to the lower prototype will overwrite the method.
Disadvantages of the prototype chain
The stereotype chain also has some disadvantages. For example, the attributes in the stereotype chain are shared. When a value in a stereotype object is modified, all the lower instances will change when fetching that value, because everyone is reading the value in the stereotype object through a pointer.
Because of this flaw, stereotype chain inheritance alone is generally not used.