The principle of prototype chain inheritance: make a subclass’s prototype object point to an instance of its parent, and when a subclass can’t find its parent, it will find its grandfather along the prototype chain.

1: Prototype chain inheritance display

Function Parent() {this.name = 'parent.prototype.getName = function () {return this.name} function Child() { } Child.prototype = new Parent() Child.prototype.constructor = Child const child = new Child console.log(child.name); console.log(child.getName());Copy the code

2: Shortcomings of prototype chain inheritance are shown

Function Parent() {this.name = [' you are so handsome ']} Parent. Prototype = function () {return this.name} function Child() { } Child.prototype = new Parent() Child.prototype.constructor = Child const child1 = new Child const child2 = New Child child1.name[0]=' console.log '(child1.name); // Peng's pectoralis major 'console.log(child2.name); //child2 has not changed, but the result is still changed -- Eddie peng's pectoralis'Copy the code

2: constructor inheritance

Supper cannot be implemented in the inheritance of prototype chain,– the failure to pass the parameter constructor to the parent class can solve the shortcoming of the inheritance of prototype chain, but it cannot inherit the methods and attributes on the parent class prototype

So, second is second after all, the key time also have to see my third

Constructor inheritance

function Parent(name) { this.name = [name] } Parent.prototype.getName = function () { return this.name } // In the constructor of a subclass, execute the constructor of the Parent class and bind this function Child() {parent.call (this, } const child1 = new Child() const child2 = new Child() console. name[0] = 'console.log'; // Peng's pectoralis major 'console.log(child2.name); / / bear big console. The log (child2 getName ()); // Methods and attributes cannot be inherited from the superclass prototypeCopy the code

3: The strongest combination inheritance

I was the last one to reach the grave

function Parent(name) { this.name = [name] } Parent.prototype.getName = function () { return this.name } function Child() { Parent.call(this, 'bear big')} Child. Prototype = new Parent () the Child. The prototype. The constructor = Child const child1 Child () = new const child2 = new Child() child1.name[0] = 'console.log(child1.name); // Peng's pectoralis major 'console.log(child2.name); / / bear big console. The log (child2 getName ()); / / big bearCopy the code

4: Parasitic inheritance

In fact, just that has shortcomings, parasitic inheritance must be the strongest!!

Pedaling pedaling, shining debut, the strongest driving!!

Well, a bunch of scum, and I’m gonna have to clean up after all, right?

function Parent(name) { this.name = [name] } Parent.prototype.getName = function () { return this.name } function Child() { Parent.call(this, Prototype = new Parent() child.prototype = new Parent() child.prototype = parent.prototype Child. The prototype. The constructor = Child const child1 Child () = new const child2 = new Child () child1. Name [0] = 'peng of pectoralis major' console.log(child1.name); // Peng's pectoralis major 'console.log(child2.name); / / bear big console. The log (child2 getName ()); / / big bearCopy the code

5: parasitic shallow copy

So there’s still a downside to parasitism? Guide by big guy, added a shallow copy, is the so-called yours is mine, mine or mine. (Direct changes are not allowed)

Upstairs of, you say what ?????? What are you cleaning up?

function Parent(name) { this.name = [name] } Parent.prototype.getName = function () { return this.name } function Child() { Parent.call(this, Prototype = new Parent() child.prototype = object.create (Parent. Prototype Child.prototype.constructor = Child const parent = new Parent() const child1 = new Child() // const child2 = new Child() // child1.name[0] = 'console.log' (child1.getName()); // console.log(child2.name); console.log(parent.getName()); //undefined to verify the success of the shallow copyCopy the code