This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Fifth, parasitic inheritance

Internally enhance the object in some way, and finally return the object

function createAnother(original) {
  var clone = Object(original); // Create a new object by calling a function
  clone.sayHi = function () {
    // Enhance the object in some way
    console.log("hi");
  };
  return clone; // Return this object
}
var person = {
  name: "Nicholas".friends: ["Shelby"."Court"."Van"]};var anotherPerson = createAnother(person);
anotherPerson.sayHi(); //"hi"
Copy the code
  • Disadvantages: can not do function reuse and reduce efficiency.
  • Scenario: This pattern applies to any function that returns a new object.

Parasitic combinatorial inheritance

function SuperType(name) {
  this.name = name;
  this.colors = ['red'.'blue'.'green'];
}
SuperType.prototype.sayName = function() {
  console.log(this.name);
}
function SubType(name, age) {
  SuperType.call(this, name); // Call SuperType() the second time
  this.age = age;
}
SubType.prototype = new SuperType(); // First call to SuperType()
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function() {
  console.log(this.age);
}
Copy the code

On the first call to the SuperType constructor, subType. prototype gets two properties: name and colors; They are both instance properties of the SuperType, but now reside in the stereotype of the SubType. When the SubType constructor is called, the SuperType constructor is called once more, this time creating instance attributes name and colors on the new object.

As you can see, there are two sets of name and colors attributes: one on the instance and one on the SubType stereotype. The solution to this problem is parasitic composite inheritance.

Parasitic combinatorial inheritance: properties are inherited by borrowing constructors, and methods are inherited by blending forms of prototype chains. The basic idea: Instead of calling a supertype constructor to specify a subtype stereotype, all we need is a copy of the supertype stereotype. Essentially: use parasitic inheritance to inherit the stereotype of a supertype and then assign the result to the stereotype of a subtype.

function inheritPrototype(subType, superType) {
  // Create object: Create a copy of the supertype prototype.
  var prototype = Object(superType.prototype); 
  // Enhance object: Add the constructor property to the created copy, replacing the default constructor property lost by overwriting the prototype.
  prototype.constructor = subType;
  // Specify an object: assign the newly created replica object to the prototype of the subtype.
  subType.prototype = prototype; 
}

function SuperType(name) {
  this.name = name;
  this.colors = ["red"."blue"."green"];
}
SuperType.prototype.sayName = function () {
  console.log(this.name);
};
function SubType(name, age) {
  SuperType.call(this, name);
  this.age = age;
}
inheritPrototype(SubType, SuperType); // Implement inheritance
SubType.prototype.sayAge = function () {
  console.log(this.age);
};
Copy the code
  1. When inheriting the stereotype property, the combination inheritance inherits the entire parent class with the stereotype chain (by assigning the instance of the parent class to the stereotype object of the subclass constructor), which gives the subclass an extra copy of the instance property of the parent class. The parasitic combinatorial inheritance only inherits the prototype property of the parent class with the original type inheritance (copies the prototype object of the parent constructor with the original type inheritance to the prototype object of the subclass constructor).
  2. The supertype constructor is called twice by composite inheritance and once by parasitic composite inheritance.
  • Advantage: Only one invocationSuperTypeConstructor to avoid theSubType.prototypeCreate unnecessary and redundant attributes above. In the meantime, the prototype chain remains unchanged and worksinstanceofisPrototypeOf().

Parasitic composite inheritance is the most ideal inheritance paradigm for reference types, but because of its late emergence, most people use the composite inheritance pattern.