1. The inheritance

ES6 did not provide us with extends inheritance, which we can implement through constructor + prototype object emulation, known as composite inheritance.

1.1 the call ()

Call this function and change the this reference at runtime

fun.call(thisArg,arg1,arg2,...)
Copy the code
  • ThisArg: the object to which this is currently called
  • Arg1, arg2: Other arguments to pass

1.2 Borrow constructors to inherit parent type attributes

Call () points this of the parent type to this of the child type, so that the child type inherits the attributes of the parent type.

Function Father(uname, age) {this.uname = name; this.age = age; Function Son(uname, age) {Father. Call (this, uname, age); } var son = new son (" Andy ", 18); console.log(son);Copy the code

1.3 Inherit parent type methods from prototype objects

Son.prototype=Father.prototype; This is problematic because if you change the child object, the parent object will change tooCopy the code

Do it right:

        Son.prototype = new Father();
Copy the code

Note:If you modify the prototype object using an object, remember to use constructor to refer back to the original constructor