1

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();
Ry-
  • 218,210
  • 55
  • 464
  • 476
Michael Kaufman
  • 683
  • 1
  • 9
  • 19

1 Answers1

2

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 }

Ry-
  • 218,210
  • 55
  • 464
  • 476