Parasitic combinatorial inheritance

const SuperClass = function () {}
const SubClass = function () {  
    SuperClass.apply(this);
}
Object.setPrototypeOf(SubClass.prototype, SuperClass.prototype);
Copy the code

Primary inheritance

const obj = {};
const subObj = Object.create(obj);
Copy the code

Description:

  • Archetypal inheritance is the acquisition of a new object based on an object.
  • Object.create(obj)Rather return a new object{}And should be__proto__Idiot property, set to obj
  • This is the most standard js inheritance behavior, is also the power of JS. Java is actually only class-oriented language, while JS is really object-oriented language! Because js itself does not have the concept of a class, only objects can describe behavior. Java inheritance copies all methods of the parent class to subclasses. But primitive inheritance, there is no copy overhead, subObj just delegates the parent object
  • In fact, primitive inheritance is a bit like the idea of composition in “inheritance vs composition”. Composition code is low invasion and flexible.

Parasitic inheritance

function inheritObj(obj, name) {  
    const subObj = Object.create(obj);  
    subObj.name = name;  
    return subObj;
}
Copy the code

Description:

  • Some books say it is a secondary encapsulation of the original type inheritance, as shown above

  • But… Black question mark face?? It doesn’t feel right to me. Because it doesn’t make any sense. What’s the difference between this and primary inheritance? There’s a member variable name, right? But almost all of the articles I found explained it that way

  • Then I found a blog post by Douglas, the man who came up with the idea of Parasitic Inheritance, Classical Inheritance in JavaScript (Crockford.com), which described it as Parasitic Inheritance

Instead of inheriting from Parenizor, we write a constructor that calls the Parenizor constructor

  • So the correct parasitic inheritance would look something like this, ahem, I chuckled

    function inheritObj(obj, name) {

    const subObj = new SubClass();

    subObj.name = name;

    }

  • This is called parasitic inheritance, because an instance of a parent class, like a parasitic beast, is quietly encapsulated in a subclass instance

  • But I looked at the little Red Book, see how to explain parasitic inheritance, and then, I found that the little Red book is using the prototype inheritance of the second packaging example, dizzy, the original front example is right, true I think more. But as the book goes on, parasitic inheritance is actually an enhancement and return of a base object, and even primitive inheritance is not required. In short, if you write an inheritObject method, pass in the base object obj, and return a new object, that’s considered parasitic inheritance, because you don’t care about types and constructors at all. What the hell, a cat catches a rat

  • In fact, Douglas’s article above, is to vomit bad traditional inheritance in JS is not very good, too cumbersome, so the concept of parasitic inheritance, he is very advocated, simple and easy to use, why not

Finally, parasitic combinatorial inheritance

  • That’s the parasitic + combination
  • Composition is constructor inheritance + stereotype chain inheritance
  • So parasitic combinatorial = parasitic inheritance + constructor inheritance + prototype chain inheritance, you learn to waste?
  • pa

All right, let’s look at the code for the parasitic combination

const SubClass = function () {    
    SuperClass.apply(this);  
}  
Object.setPrototypeOf(SubClass.prototype, SuperClass.prototype);
Copy the code
  • SuperClass.apply(this); Constructor inheritance

  • SubClass. Prototype is the inheritance of the prototype chain

  • SuperClass. Prototype is used as a benchmark object to enhance subclass. prototype, which is the idea of parasitic inheritance

  • So 1 + 1 + 1 = 3. Did you fail?

  • Prototype = Object. Create (superclass.prototype)

  • Finally, finally, in fact, there is no performance improvement in writing this line of code, but in the moment of writing this line of code, the pursuit of perfect code is satisfied for a moment, like the light of the right way, shining on the front. I seem to understand