This is the 11th day of my participation in the August Wenwen Challenge.More challenges in August

First look at the ES6 inheritance source:

// ES6
class Parent{
    constructor(name){
        this.name = name;
    }
    static sayHello(){
        console.log('hello');
    }
    sayName(){
        console.log('my name is ' + this.name);
        return this.name; }}class Child extends Parent{
    constructor(name, age){
        super(name);
        this.age = age;
    }
    sayAge(){
        console.log('my age is ' + this.age);
        return this.age; }}let parent = new Parent('Parent');
let child = new Child('Child'.18);
console.log('parent: ', parent); // parent: Parent {name: "Parent"}
Parent.sayHello(); // hello
parent.sayName(); // my name is Parent
console.log('child: ', child); // child: Child {name: "Child", age: 18}
Child.sayHello(); // hello
child.sayName(); // my name is Child
child.sayAge(); // my age is 18
Copy the code

Parasitic combinatorial inheritance (common)

Fixed an issue with combination inheritance

Parasitic:

  • Returns an object within a function and then calls

Combination:

1. The prototype of a function is equal to another instance.

2. Use apply or call to introduce another constructor that takes arguments

Prototype chain inheritance

Use an instance of a parent class as a prototype for a subclass

Features:

  • Instances of subclasses are also instances of superclasses
  • It is convenient to inherit methods from the stereotype of the parent type, but inheritance of attributes is meaningless

Disadvantages:

  • It is executed only once and cannot pass values to properties
  • The inheritance of attributes is meaningless

Borrow constructor inheritance

Call the parent class inside the subclass, and change the reference to this in the parent class with call, which copies the instance attributes of the parent class to the subclass

Features:

  • When you create a subclass instance, you can pass parameters to the parent class
  • Multiple inheritance can be implemented
  • You can easily inherit attributes from a parent type, but not methods from a stereotype

Disadvantages:

  • An instance is not an instance of a parent class, only an instance of a subclass
  • Cannot inherit methods from the stereotype
  • Function reuse is not possible, each subclass has a copy of the parent class instance function, affecting performance

Composite inheritance (composite stereotype chain inheritance and borrowed constructor inheritance)

It combines the advantages of both modes, parameter transfer and reuse

Features:

  • Can inherit the attributes of the parent class prototype, can pass parameters, reusable.
  • The constructor properties introduced by each new instance are private.

Disadvantages:

  • If the parent constructor is called twice (memory consuming), the subclass constructor replaces the prototype parent constructor.

Primary inheritance

Wrapping an object with a function and then returning a call to the function makes the function an instance or object that can be added any properties you want. Object.create () works this way.

Features:

  • It’s like copying an object and wrapping it with a function.

Disadvantages:

  • All instances inherit properties from the stereotype.
  • Reuse cannot be achieved. (New instance attributes are added later)

Parasitic inheritance

It’s just a case of the original inheritance.

Features:

  • Instead of creating a custom type, this function automatically becomes the new object being created because it just returns the object (this one) around the shell.

Disadvantages:

  • No prototype, no reuse.