2016-12-27 67 views
0

我嘗試使用內模板字面超()在我的課超好,這裏是超類代碼:模板文字與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函數,但它不允許我在控制檯中使用此代碼:

'超'關鍵字意外

任何指導將不勝感激。

+0

的'super'關鍵字只有在非常特殊的情況下有效。所有這些情況都列在[documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super)中。 – 4castle

+0

如果有人回答您的問題,請單擊旁邊的空白複選標記以將其標記爲正確。否則,改進問題。 –

回答

2

你想super.doWork(),不super()