What is inheritance:

By inheriting from B, object A can directly own all properties and methods of object B.


Prototype chain inheritance

Key point: The prototype of a subtype is an instance object of the parent type, and the subclass inherits the attributes and methods of the parent class by taking the private attributes and public methods of the parent class as its own public attributes and methods

Function Staff() {this.pany = 'tianchuang'; this.list = []; } / / the prototype of the parent class Staff. The prototype. GetComName = function () {return this.com pany; }; // Subclass function Coder(name, skill) {this.name = name; this.skill = skill; } // Staff Coder. Prototype = new Staff(); / / because subclass prototype to has changed, so need to subclass the prototype of its constructor to subclass itself Coder. The prototype. The constructor = Coder; Prototype. GetInfo = function() {return {name: this.name, skill: this.skill}; }; Let coder = new coder (' xiaoming ', 'javascript'); coder.getInfo(); // {name: 'small ', skill: 'javascript'} decoder.getComName (); // 'tianchuang'Copy the code

The class extend inheritance

With the concept of classes in ES6, you can declare a class through class and implement inheritance through the extends keyword.

class Parent { constructor(name) { this.name = name; } static say() { return 'hello'; } } class Child extends Parent { constructor(name, age) { super(name); Constructor (name) this.age = age; } } var child1 = new Child('kevin', '18'); console.log(child1);Copy the code