OutputNot attempted
private # fields vs _protected
class Machine { #water = 0; _power = 100; set water(v) { this.#water = v < 0 ? 0 : v; } get water() { return this.#water; } static hasWater(obj) { return #water in obj; }}class Mega extends Machine { boost() { this._power += 50; return this._power; }}const m = new Mega();m.water = -5;console.log(m.water, m.boost(), Object.keys(m));console.log(Machine.hasWater(m), Machine.hasWater({}));Spaces and quote style don’t matter · write a thrown error as TypeError
Ctrl Enter
Show answer & explanation
The expected output comes from actually running the snippet; opening this shows it next to the explanation.
#fields are truly private: not in Object.keys, not reachable from outside, and not even from subclasses directly (only via the parent's methods). _power is a convention: an ordinary public property. #x in obj is the brand check for "was this made by my class?"