ES5的继承:实质是先 创造 子类的实例对象this,然后再将父类实的方法添加到this上( Parent.apply(this) )
function Parent(name){
this.name = name;
}
function Child(...args){
console.log(this); // Child {} 先创建子类的this
Parent.apply(this,args);// 通过这一步 就是将父类调用一遍 apply(this)的this指向全局 Parent
console.log(this); // Child {name: 'zhangsan'} 再添加父类实例属性和方法
}
Child.prototype = Object.create(Parent.prototype)
Child.prototype.constructor = Child;
const child1 = new Child('zhangsan')
console.log(child1.name);
ES6的继承:实质是先将父类实例对象的属性和方法,添加到this上(所以在使用this之前必须先调用super方法),再用子类的构造函数修改this。