使用super:super继承,super方法,super对象。
1.super继承,在constructor中调用super,子类没有定义constructor方法,super方法会被默认添加。子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错
。(因为:ES5 的继承的实质是先创造子类的实例对象this,然后再将父类的方法添加到this上面(Parent.apply(this))。ES6 的继承机制完全不同,实质是先将父类实例对象的属性和方法,加到this上面(所以必须先调用super方法),然后再用子类的构造函数修改this。)
2.super方法:作为函数时,super()只能用在子类的构造函数之中
3.super作为对象时,在普通方法中,指向父类的原型对象,在静态方法中,指向父类。
super的介绍
https://zhuanlan.zhihu.com/p/361387348
对于对象中使用super:
son1.func()输出jack;son2.func()出错。
son2.func()出错的原因是,在对象son2,定义的func方法是传统的定义方式,不是es6新定义的方法的简写方式,func(){};传统函数表达式没有加入对super的支持。
const father={
func(){
console.log('jack')
}
};
const son1={
__proto__:father,
func(){
super.func();
}
};
// Object.setPrototypeOf(son1, father);
son1.func();//jack
const son2={
// __proto__:father,
func:function()
{
super.func();
}
};
Object.setPrototypeOf(son2, father); //此句等于第16行的设置原型对象
son2.func()//出错