문제

I'm trying to learn more about ES6 and saw this class in a tutorial. Why does goFast() work without a function keyword in front of it? Is this a new shorthand for functions in classes, or…?

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();
도움이 되었습니까?

해결책

Is this a new shorthand for functions in classes

Yes; see the ES6 draft for the definitions of the various relevant parts of the grammar:

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top