模板文字与ES6类
问题描述:
我尝试使用内模板字面超()在我的课超好,这里是超类代码:模板文字与ES6类
class Person{
constructor(name){
this.name = name;
}
get name(){
return this._name;
}
set name(newVal){
this._name = newVal;
}
doWork(){
return `${this.name} is coming from person` ;
}
}
,这是子类:
class Employee extends Person{
constructor(name, title){
super(name);
this.title = title;
}
get title(){
return this._title;
}
set title(newVal){
this._title = newVal;
}
doWork(){
console.log(this.name);
return `${super()} ${this.name}`; //here is my issue
}
}
我试图在使用模板文字时引用子类中的Person类中的doWork函数,但它不允许我在控制台中使用此代码:
'超'关键字意外
任何指导将不胜感激。
答
你想super.doWork()
,不super()
的'super'关键字只有在非常特殊的情况下有效。所有这些情况都列在[documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super)中。 – 4castle
如果有人回答您的问题,请单击旁边的空白复选标记以将其标记为正确。否则,改进问题。 –