Using es6 arrow functions inside class

前端 未结 3 1962
孤独总比滥情好
孤独总比滥情好 2020-12-10 20:57

When i change a function draw(){ //} to draw = () => { // } I am getting an error like \"Uncaught SyntaxError: Unexpected token =\". What may be

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 21:40

    You will need to use babel's Class properties transform, which will transpile:

    class Picture {
      draw = () => {
        console.log('drawing')
      }
    }
    

    into:

    class Picture {
      constructor() {
        this.draw = () => {
          console.log('drawing');
        };
      }
    }
    

    You can try it in this repl (make sure to enable the class properties transform plugin)

提交回复
热议问题