据我所知
super([参数]( 关键字调用父构造函数
具有以下基本类:
class X{
constructor(){
return 'x'; // doesn't matter when calling super()
}
}
class Z extends X{
constructor(){
console.log('This will return the newly created object invoked by new keyword', super())
}
}
new Z; // Z {}
为什么super()
返回全新的Z对象?
不能从构造函数返回基元类型。
class X{
constructor(){
// to set correct prototype
return Object.setPrototypeOf(new String("x"), new.target.prototype);
// or
// return new String('x') just to show you could return any object
}
}
class Z extends X{
constructor(){
console.log('This will return the newly created object invoked by new keyword', super())
}
}
console.log(new Z); // Z {}