Common inheritance is divided into two kinds, an interface inheritance, an inheritance method signature, an implementation inheritance, and an actual method of inheritance. JS only supports the latter one.
1 prototype chain
First, we look at the relationship between prototypes, constructors, and instances. If we make the prototype object of one function equal to another instance, then another prototype object equals another instance, and so on, it forms the prototype chain.
Code:
function Supertype () {
This.name=true;
}
Supertype.prototype.getvalue=function () {
return this.name;
}
Function subtype () {
this.age=12;
}
Subtype.prototype=new supertype (); Build the prototype chain and let subtype inherit all the properties and methods of Supertype.
var instance=new subtype ();
Alert (Instance.getvalue ());//true
2 borrowing constructors
Use the call () or the Apply () method
function Supertype () {
this.color=["Red", "Blue"];
}
Function subtype () {
Supertype.call (this);
}
var instance1=new subtype ();
Borrowing a constructor does not change the prototype, and each instance has its own copy of the property.
JavaScript inheritance Detailed