1

我正在尝试更多地了解 ES6,并在教程中看到了这门课。为什么前面goFast()没有关键字就可以工作?function这是类中函数的新简写,还是……?

class RaceCar extends Car { //inheritance
  constructor(make, topSpeed) {
    super(make); //call the parent constructor with super
    this.topSpeed = topSpeed;
  }

  goFast() {
    this.currentSpeed = this.topSpeed;
  }
}

let stang = new RaceCar('Mustang', 150);
stang.printCurrentSpeed();
stang.goFast();
stang.printCurrentSpeed();
4

1 回答 1

2

这是类中函数的新简写吗

是的; 有关语法各个相关部分的定义,请参见ES6 草案:

ClassBody:ClassElementList
ClassElement:MethodDefinition
MethodDefinition:PropertyName(StrictFormalParameters){FunctionBody}

于 2014-05-03T18:25:11.247 回答