Question

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();
Was it helpful?

Solution

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 }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top