Inheritance is a concept in object-oriented software technology, which, along with polymorphism and encapsulation, constitutes the three basic characteristics of object-oriented programming. Inheritance allows a subclass to have the properties and methods of its parent class, or to redefine, append properties and methods, etc.
By setting the prototype object of the subclass to the instance of the parent class, inheritance of accessing parent class properties and methods is achieved.
// Define the parent classfunctionParent(){this.name ="parent";this.say=function(){ console.log(this.name);}}// Define the child classfunctionChild(){this.name ="child";}Child.prototype =newParent();// Set the prototype object of the child class to the instance of the parent classChild.prototype.constructor = Child;// Fix constructor to comply with the prototype chain rulesvar child =newChild();// Instantiate the child classchild.say();// child // At this point, the subclass can access the parent class's say method. When looking for the name property, it first looks in its own properties and finds it, so it does not search upwards. If the subclass does not have the name member, it will print parentconsole.log(child instanceofParent);// true // Check if the Child constructor's prototype object is in the Parent's prototype chain
When the subclass constructor is called, the parent class constructor is invoked using call or apply to extend this.
// Define the parent classfunctionParent(from){this.name ="parent";this.say=function(){ console.log(this.name);}this.from = from;}// Define the child classfunctionChild(from){Parent.call(this, from);// Call the parent class constructor and bind 'this' to extend the Child instance members, and pass parametersthis.name ="child";}var child =newChild("child");// Instantiate the child classchild.say();// childconsole.log(child.from);// child
The instance is not an instance of the parent class, only of the subclass.
Only inherits the parent class's constructor properties and methods, not the parent class's prototype properties and methods.
Each subclass has a copy of the parent class instance function, it copies the parent class function instead of referencing it, which affects performance.
Combines prototype chain inheritance and constructor borrowing inheritance, combining the advantages of both patterns for parameter passing and reusability.
// Define the parent classfunctionParent(from){this.name ="parent";this.say=function(){ console.log(this.name);}this.from = from;}// Define the child classfunctionChild(from){Parent.call(this, from);this.name ="child";}Child.prototype =newParent();Child.prototype.constructor = Child;var child =newChild("child");// Instantiate the child classchild.say();// childconsole.log(child.from);// child
Calls the parent class constructor twice, generating two instances, and the subclass's constructor copy will replace the instance of the parent class constructor on the prototype.
By parasitic means, eliminate the instance properties of the parent class. When calling the parent class constructor twice, the initialization of instance methods and properties will not be repeated to avoid the shortcomings of combination inheritance.
// Define the parent classfunctionParent(from){this.name ="parent";this.say=function(){ console.log(this.name);}this.from = from;}// Define the child classfunctionChild(from){Parent.call(this, from);this.name ="child";}varf=function(){};// Create a class without instance methodsf.prototype =Parent.prototype;// Shallow copy the parent class prototypeChild.prototype =newf();// Instantiate f, at this point no instance method is called, while establishing the prototype chainChild.prototype.constructor = Child;var child =newChild("child");// Instantiate the child classchild.say();// childconsole.log(child.from);// child