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

Combination of inheritance

Composite inheritance, also known as pseudo-classical inheritance, combines the archetypal chain and embezzle constructors we talked about yesterday, combining the best bits of both. The basic idea is to use the stereotype chain to inherit attributes and methods on the stereotype, and to inherit instance attributes by stealing the constructor. The advantage of this is that you can reuse method definitions on the stereotype, and each instance has its own attributes.

    function SuperType (name) {
        this.name = name;
        this.colors = ["red"."yellow"."bule"];
    }
    SuperType.prototype.sayName = function(){
        console.log(this.name)
    }
    function SubType(name,age){
        SuperType.call(this,name);
        this.age = age;
    }
    SubType.prototype = new SuperType();
    SubType.prototype.sayAge = function(){
        console.log(this.age);
    }
    let instancel = new SubType("jackson".22);
    instancel.colors.push("pink");
    instancel.sayName(); // "jackson"
    instancel.sayAge();/ / 22
    console.log(instancel.colors);// ["red", "yellow", "bule", "pink"]
    
    let instance2 = new SubType("bear".20);
    console.log(instance2.colors); // ["red", "yellow", "bule"]
    instance2.sayName(); // "bear";
    instance2.sayAge(); / / 20
Copy the code

SubType calls SuperType, passing in name and defining its own property age. Subtype. prototype is also assigned a SuperType instance. Adding the sayage method to the stereotype after the stereotype is assigned creates two subType instances that have their own properties and can share the same methods.

Composite inheritance is the most commonly used inheritance pattern in JS.

Parasitic inheritance

Parasitic inheritance is wrapping an object with a function and then returning the call to that function. The function becomes an instance or object that can add attributes at will. This is how object.create() works.

  // Parasitic inheritance
    function subobject(obj) {
        let clone = Object(obj);
        clone.sayName = function(){
            console.log("jackson")};return clone;
    }
    let sub = {
        name:"bear"
    }
    let sup = subobject(sub);
    sup.sayName();//jackson
Copy the code

This example returns a new object based on the sub object. The returned sup object has the sub property and method, and a new method, sayName().

Parasitic inheritance is also suitable for scenarios where the focus is on the object, not the type or constructor. The object() function is not required for parasitic inheritance; any function that returns a new object can be used here.

Note that adding a function to an object through parasitic inheritance makes the function difficult to reuse, similar to the constructor pattern.

Parasitic combination inheritance

The efficiency problem with composite inheritance is that its superclass constructor is always called twice, once when the word class prototype is created and once in the subclass constructor. Essentially, subclasses just need to rewrite their own prototypes at execution time.

     function inheritPrototype(subType, superType) {
        let prototype = Object(superType.prototype); // Create an object
        prototype.constructor = subType; // Enhance the object
        subType.prototype = prototype; // Assign objects
    }
Copy the code

The inheritPrototype() function implements the core logic of parasitic composition inheritance. This function takes two arguments: the subclass constructor and the superclass constructor. Inside this function, the first step is to create a copy of the superclass prototype. We then set the constructor property to the returned Prototype object, resolving the problem that default constructor was lost due to overwriting the prototype. Finally, the newly created object is assigned to the prototype of the subtype. As shown in the following example, calling inheritPrototype() implements the subtype stereotype assignment in the previous example:

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);
    SubType.prototype.sayAge = function () {
        console.log(this.age);
    };
Copy the code

The SuperType constructor is called only once, avoiding unnecessary and unneeded properties on subType. prototype, so this example is arguably more efficient. And the prototype chain remains the same.

Parasitic composite inheritance can be the best model for reference type inheritance.