简体   繁体   中英

Template literal with super in ES6 class

I am trying to use super() inside template literal in my class, here's the super class code:

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` ;
    }
}

and this is the child class:

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
    }
  }

I am trying to reference the doWork function in the Person class inside the child class while using template literals, but it's not allowing me, I have this in console:

'super' keyword unexpected here

Any guidance would be appreciated.

您需要super.doWork() ,而不是super()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM