Borrow constructor inheritance
In solving the problem of including reference type values in a prototype, developers began to use a technique called borrow constructor stealing (sometimes called forged objects or classical inheritance). The basic idea of this technique is quite simple, namely, calling the supertype constructor inside the subtype constructor.
Basic mode
function SuperType(){ this.colors = ["red", "blue", "green"];}function SubType(){ //Inherited SuperType SuperType.call(this);}var instance1 = new SubType();instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"var instance2 = new SubType();alert(instance2.colors); //"red,blue,green"Basic ideas
The basic idea of borrowing constructors is to use call or apply to copy (borrow) the properties and methods specified in the parent class into the instance created by the subclass. Because this object is bound at runtime based on the execution environment of the function. That is to say, globally, this is equal to window, and when a function is called as a method of an object, this is equal to that object. The call and apply methods can be used to call a method instead of another object. The call and apply methods can change the object context of a function from the initial context to a new object specified by thisObj.
Therefore, this borrowing constructor is, when the new object is called (note that the new operator is different from the direct call. When the function is called, this points to the window. When the new is created, this points to the created instance), a new instance object is created and the code in SubType is executed. The call in SubType calls SuperTyep, which means that this pointing is changed to pointing to a new instance, so this related attributes and methods in SuperType will be assigned to the new instance, rather than assigning to SupType. All instances have these properties and methods of this defined by the parent class.
Advantages
Compared with prototype chains, borrowing constructors has a great advantage, that is, they can pass parameters to supertype constructors in subtype constructors. Because the attribute is bound to this, it is assigned to the corresponding instance when called, and the values of each instance will not affect each other.
For example:
function SuperType(name){this.name = name;}function SubType(){//Inherits SuperType, and also passes the parameter SuperType.call(this, "Nicholas");//Instance attribute this.age = 29;}var instance = new SubType();alert(instance.name); //"Nicholas";alert(instance.age); //29Disadvantages
If you just borrow a constructor, then the problems with the constructor pattern will not be avoided - the 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, and as a result, all types can only use constructor patterns. Given these issues, the technique of borrowing constructors is also rarely used alone.
Combination inheritance
Combination inheritance, sometimes called pseudo-classic inheritance. It is an inheritance model that combines the prototype chain and borrowed constructor technology into one piece, so as to play the strengths of both.
Basic ideas
The idea is to use the prototype chain to implement the inheritance of prototype properties and methods, and to implement the inheritance of instance properties by borrowing constructors. In this way, function multiplexing is achieved by defining methods on the prototype, and it can also ensure that each instance has its own attributes.
Basic Model
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.constructor = SubType;SubType.prototype.sayAge = function(){alert(this.age);};var instance1 = new SubType("Nicholas", 29);instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"instance1.sayName(); //"Nicholas";instance1.sayAge(); //29var instance2 = new SubType("Greg", 27);alert(instance2.colors); //"red,blue,green"instance2.sayName(); //"Greg";instance2.sayAge(); //27Advantages
Combination inheritance avoids the defects of prototype chains and borrowed constructors, combines their advantages, and becomes the most commonly used inheritance pattern in JavaScript.
Disadvantages
The biggest problem with combined inheritance is that in any case, the supertype constructor will be called twice: once when creating a subtype prototype, and the other time inside the subtype constructor. While subtypes will eventually contain all instance properties of the supertype object, we have to override these properties when calling the subtype constructor.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.