2. Borrow constructor
To solve the problem caused by including reference type values in the prototype, we use a technique called borrow constructor stealing (also called forged objects or classical inheritance).
The basic idea of this technique: call the supertype constructor inside the subclass constructor.
The constructor can be executed on the newly created subclass object by using the apply() and call() methods.
function SuperType(){ this.colors = ["red", "blue", "green"];}function SubType(){ //Inherited SuperType SuperType.apply(this);}var instance1 = new SubType();instance1.colors.push("black");alert(instance1.colors); //red,blue,green,blackvar instance2 = new SubType();alert(instance2.colors); //red,blue,greenIn the above example, the SuperType constructor is actually called in the environment of the newly created SubType instance (instance1 instance2). This way, all object initialization codes defined in the SuperType() function will be executed on the new SubType object. So each instance of Subtype will have its own copy of the colors attribute.
Pass parameters
For prototype chains, borrowing constructors has a great advantage, that is, you can pass parameters in supertype constructors in subtype constructors.
function SuperType(name){ this.name = name;}function SubType(){ SuperType.call(this, "Bob"); this.age = 18;}var instance1 = new SubType();alert(instance1.age); //18alert(instance1.name); //BobProblem of borrowing constructors:
Methods are all defined in the constructor, so there is no way to talk about function reuse. Moreover, methods defined in supertype prototypes are also invisible to subtypes.
3. Combination inheritance
Combination inheritance, sometimes called pseudo-classical inheritance, refers to combining prototype chains and borrowing constructor techniques together. This is a mode of inheritance that plays the strengths of both.
Use prototype chains to implement inheritance of prototype properties and methods;
Inheritance of instance properties is achieved by borrowing constructors.
In this way, function reuse is achieved by defining methods on the prototype, and it can also ensure that each instance has its own attributes.
function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"];}SuperType.prototype.sayName = function(){ alert(this.name);}function SubType(name, age){ //Inheritance attribute SuperType.call(this, name); this.age = age;} //Inheritance method SubType.prototype = new SuperType(); SubType.prototype.sayAge = function(){ alert(this.age);}var instance1 = new SubType("Bob", 22);instance1.colors.push("black");alert(instance1.colors); //red,blue,green,blackenstance1.sayName(); //Bobinstance1.sayAge(); //22var instance2 = new SubType("Alice", 21);alert(instance2.colors); //red,blue,greeninstance2.sayName(); //Aliceinstance2.sayAge(); //21In this example, the SuperType constructor defines two properties: name and colors. The prototype of SuperType defines a method saysName().
The SubType constructor passes in the name parameter when calling the SuperType constructor and defines its own attribute age. Then assign the instance of SuperType to the prototype of SubType. The method saysAge() is defined on this prototype.
This allows two different SubType instances to have their own properties - including colors attributes, and the same method can be used.
The above article briefly talks about JS inheritance_borrow constructors & combination inheritance is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.