Javascript Syntax Ask

Asked by At

I am studying the core javascript, and met the following code (https://javascript.info/private-protected-properties-methods#protecting-wateramount)

class CoffeeMachine {
  waterAmount = 0; // the amount of water inside

  constructor(power) {
    this.power = power;
    alert(`Created a coffee-machine, power: ${power}`);
  }

}

// create the coffee machine
let coffeeMachine = new CoffeeMachine(100);

// add water
coffeeMachine.waterAmount = 200;

Line 2, waterAmount = 0 run in browser environment, but not in node environment, saying

  waterAmount = 0;
              ^

SyntaxError: Unexpected token =
    at new Script (vm.js:80:7)
    at createScript (vm.js:274:10)

How come? So line 2 is not a good practice, I assume? Thanks for shedding light.

0 Answers