ES5, ES6 inheritance mode

Objectives of inheritance:

  • Parent class public properties and methods are subclass public properties and methods
  • Superclass private properties and methods are subclass private properties and methods

1. The ES5 inheritance

  • Prototype inheritance
Function Parent(){this.familyName = "Corleone"} parent.prototype. skill = ()=>{return "protect family" Child () function {enclosing name = "Michael"} / / subclass prototype points to the Parent class instance Child. The prototype = new Parent () the Child. The prototype. The constructor = Child // note: Ability = ()=>{return "resourcey"} let michey = new Child() console.log(michey.name, michey) Michey.familyname, Michey.skill (), michey.ability())) // Michael Corleone protects his familyCopy the code

Stereotype inheritance refers to a subclass stereotype pointing to an instance of the parent class to inherit public and private attributes and methods of the parent class. The defect is that both parent public and private become subclass public.

  • .call() Parasitic inheritance
Function Parent(){this.familyName = "Corleone"} parent.prototype. skill = ()=>{return "protect family" Function Child(){this.name = "Michael"; // Use the Parent constructor as a normal function and change this to parent.call (this)} // Note: Ability = ()=>{return "resourcey"} let michey = new Child() console.log(michey.name, michey) Michey.familyname, Michey.skill (), michey.ability())) // Michael Corleone protects his familyCopy the code

Parasitic inheritance is when the parent constructor is executed as a normal function within the subclass constructor, changing the this pointer to point to the subclass to complete inheritance. The defect is that the public and private attributes of the parent class and the methods are private to the subclass.

  • Combinatorial inheritance (.call() + object.create)

The object.create () method creates an empty Object, and the first argument points the __proto__ of the created empty Object to the passed parameter Object

Function Parent(){this.familyName = "Corleone"} parent.prototype. skill = ()=>{return "protect family" Function Child(){this.name = "Michael"; Call (this)} Child. Prototype = object.create (Parent. Prototype) Ability = ()=>{return "resourcey"} let michey = new Child() console.log(michey.name, michey) Michey.familyname, Michey.skill (), michey.ability())) // Michael Corleone protects his familyCopy the code

Superclass public properties and methods are subclass public properties and methods, and superclass private properties and methods are subclass private properties and methods

2. ES6 class inheritance

class Parent{ constructor(name, generation){ this.name = name this.generation = generation } info(){ return `${this.name} ${this. Generation} class Child extends Parent{constructor(name, generation, end){super(name, constructor){constructor(name, generation, end){} Generation) this.end = end} skill(){return "tiaoanleng"}} let Vito = new Child(' tiaoanleng ', 1, 1) Let michey = new Child('Michael', 2, 'white ')Copy the code