Why use the method of prototype inheritance to inherit the prototype of the parent class in parasitic combinatorial inheritance in JS?

I just started to learn that js, saw the section of parasitic combinatorial inheritance on "js elevation", but I don"t quite understand the code given in the book.
what I understand is that parasitic combinatorial inheritance is to avoid the problem of duplicating instance properties and prototype objects in combinatorial inheritance. It inherits the properties in the parent constructor by borrowing the constructor and inherits the prototype object in the parent class using the prototype inheritance method.
this is the code from the book:

function inheritPrototype(SubType,SuperType){
    var pro = Object.create(SuperType.prototype);
    pro .constructor = SubType;
    SubType.prototype = pro ;
    }

but how is it different from the following function?

function inheritPrototype(SubType,SuperType){
    SubType.prototype = SuperType.prototype;
    SubType.prototype.constructor = SubType;
    }

I don"t quite understand why we use the method of prototype inheritance to inherit the prototype of the parent class. Isn"t prototype inheritance a shallow copy? What"s the difference between this and direct assignment?

Mar.21,2021

The code above, if you add methods to the prototype of SubType , for example:

SubType.prototype.fn = function(){...}

this does not affect SuperType.prototype .

the following code will.


are you talking about modifying the method of constructor inheritance + prototype object inheritance? both of them will have disadvantages if used alone. For example, if you use prototype object inheritance alone, the disadvantage is that there is no way to pass parameters. We know that modifying constructor inheritance completes inheritance by changing this to complete inheritance. For example, if you use modified constructor inheritance alone, then there is no way to get the method on the prototype object, so combinatorial inheritance is a way to take its essence and discard its dross

Menu