5. Parasitic inheritance
Similar to parasitic constructors and factory patterns, create a function that is only used to encapsulate the inheritance process, which internally enhances the object in some way and finally returns the object.
function createAnother(original){ var clone = Object.create(original); //Create a new object by calling the function clone.sayHi = function(){ //Enhance this object in some way alert("Hi"); }; return clone; //Return this object}var person = { name: "Bob", friends: ["Shelby", "Court", "Van"]};var anotherPerson = createAnother(person); anotherPerson.sayHi();In the above example, the createAnother function receives a parameter, that is, the object to be used as the basis of the new object.
AnotherPerson is a new object created based on person. The new object not only has all the properties and methods of person, but also its own sayHi() method.
6. Parasitic combination inheritance
Combination inheritance is the most commonly used inheritance pattern in js. The biggest problem with combination inheritance is that the constructor will be called twice in any case: once when creating a subtype prototype, and the other time inside the subtype constructor.
function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"];}SuperType.prototype.sayName = function(){ alert(this.name);}function SubType(name, age){ SuperType.call(this, name); //The second call to SuperType() this.age = age;}SubType.prototype = new SuperType(); //The first call to SuperType()SubType.prototype.sayAge = function(){ alert(this.age);}When the SuperType constructor is called for the first time, SubType.prototype will get two properties: name and colors; they are both instance properties of SuperType, but they are now located in the SubType prototype.
When the SubType constructor is called, the SuperType constructor is called again, and this time, the instance attribute name and colors are created on the new object.
So these two attributes block two attributes of the same name in the prototype.
Parasitic combination inheritance is to solve this problem.
Inherit attributes by borrowing constructors;
Inherit the method through the prototype chain.
There is no need to call the supertype constructor to specify the prototype of the subtype.
function inheritPrototype(subType, superType){ var protoType = Object.create(superType.prototype); //Create object protoType.constructor = subType; //Enhance object subType.prototype = protoType; //Specify object}function SuperType(name){ this.name = name; this.colors = ["red", "blue", "green"];}SuperType.prototype.sayName = function(){ alert(this.name);}function SubType(name, age){ SuperType.call(this, name); //The second call to SuperType() this.age = age;}inheritPrototype(SubType, SuperType)SubType.prototype.sayAge = function(){ alert(this.age);}var instance = new SubType("Bob", 18);instance.sayName();instance.sayAge();inheritPrototype function receives two parameters: the subtype constructor and the supertype constructor.
1. Create a copy of the supertype prototype.
2. Add constructor attribute to the created copy to make up for the default constructor attribute lost due to rewriting the prototype.
3. Assign the newly created object (i.e. copy) to the prototype of the subtype. This method only calls the SuperType constructor once, and instanceof and isPrototypeOf() can also be used normally.
The above article briefly talks about JS inheritance_parasitic inheritance & parasitic 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.