JS Object Oriented Inheritance Detailed Explanation of Multiple Combination Inheritance

  • 2021-07-02 23:08:34
  • OfStack

This time, we should talk about combination, prototype, parasitic and parasitic combination inheritance.

1. Combination inheritance: Also called pseudo-classical inheritance, it refers to a kind of inheritance mode that combines prototype chain and borrowed constructor technology in one block.

Let's look at an example:


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);
    this.age = age;
  }

  // Inheritance method 
  SubType.prototype = new SuperType();
  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(); //29

  var instance2 = new SubType("Greg", 27);
  alert(instance2.colors); //red,blue,green
  instance2.sayName(); //Greg
  instance2.sayAge(); //27

Combinatorial inheritance avoids the defects of prototype chain and borrowing constructor, and integrates their advantages.

2. Prototype inheritance

Inheritance can be implemented without having to predefine a constructor, and its essence is to perform a shallow copy of a given object. The copy obtained by copying can also be transformed one step further.


function object(o) {
    function F(){};
    F.prototype = o;
    return new F;
  }

  var person = {
   name: "Nicholas",
   friends: ["Shelby", "Court", "Van"]
  };

  var antherPerson = object(person);
  antherPerson.name = "Greg";
  antherPerson.friends.push("Rob");

  var antherPerson = object(person);
  antherPerson.name = "Linda";
  antherPerson.friends.push("Barbie");

  alert(person.friends); //Shelby,Court,Van,Rob,Barbie

3. Parasitic inheritance

Much like prototype inheritance, it creates an object based on an object or some information, then enhances the object, and finally returns the object. In order to solve the inefficiency problem caused by calling supertype constructor many times, this pattern can be used together with composite inheritance.


function object(o) {
    function F(){};
    F.prototype = o;
    return new F;
  }
  function createAnother(original) {
    var clone = object(original);
    clone.sayHi = function() {
      alert("Hi");
    };
    return clone;
  }

  var person = {
    name: "Nicholas",
    friends: ["Shelby", "Court", "Van"]
  };

  var anotherPerson = createAnother(person);
  anotherPerson.sayHi();

4. Parasitic combined inheritance

Combining the advantages of parasitic inheritance and combined inheritance is the most effective way to realize basic type inheritance.


// Inheritance prototype 
  function extend(subType, superType) {
    function F(){};
    F.prototype = superType.prototype;

    var prototype = new F;
    prototype.constructor = subType;
    subType.prototype = prototype;
  }

  // Superclass method 
  function SuperType(name) {
    this.name = name;
    this.colors = ["red", "blue", "green"];
  }
  SuperType.prototype.sayName = function() {
    return this.name;
  }

  // Subclass method 
  function SubType(name, age) {
    SuperType.call(this, name);
    this.age = age;
  }

  // Prototype of inheritance superclass 
  extend(SubType, SuperType);

  // Subclass method 
  SubType.prototype.sayAge = function() {
    return this.age;
  }

  var instance1 = new SubType("Shelby");
  var instance2 = new SubType("Court", 28);

  instance1.colors.push('black');

  alert(instance1.colors); //red,blue,green,black
  alert(instance2.colors); //red,blue,green

  alert(instance1 instanceof SubType); //true
  alert(instance1 instanceof SuperType); //true

The efficiency of this example is that it only calls the SuperType constructor once, and thus avoids creating unnecessary and redundant properties on SubType. prototype. At the same time, the prototype chain can remain unchanged. Therefore, instanceof and isPrototypeOf () can also be used normally. Developers generally believe that parasitic combined inheritance is the most ideal inheritance paradigm for reference types.


Related articles: