据我所知
super([arguments])关键字调用父构造函数
有这些基本课程:
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 对象?
答案 0 :(得分:2)
您无法从构造函数中返回基本类型。
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 {}