# Self-implementing inheritance (one-time inheritance constructor) //the constructor of the parent class function father (name,age) { this.name= name this.age=age } //the prototype object of the parent class father.prototype={ constructor:father, say:function () { console.log (' I am ' +this.name+ ' , ' +this.age+ ' years old ') } } //class Inheritance: Inherits only the parent class template, does not inherit the prototype object (inherits from the method implementation of the constructor) //the constructor of the subclass function child (Name,age) { //call apply Extended Parent template usage range Child.superClass.constructor.call (this,name,age) } function extend (sub,sup) { //initialization: Create empty function, empty template and empty prototype Object var f=new function () // Empty function: Inherit only prototype object of parent class f.prototype=sup.prototype //subclass inherits empty function, can implement inherit only prototype object, do not inherit constructor sub.prototype=new f () //also primitive constructors for atomic classes sub.prototype.constructor=sub // Save the parent prototype object, role 1: Decoupling, Function 2: Easy to get the parent class prototype object sub.superclass=sup.prototype } extend (Child,father) var c1=new child (' li4 ', +) c1.say () console.log (Father.prototype.isPrototypeOf (C1))
Algorithm-javascript self-Implementation inheritance extend (single-Inheritance prototype object and single-inheritance constructor)