Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
class Temperature { #celsius; constructor(celsius) { this.#celsius = celsius; } get celsius() { return this.#celsius; } set celsius(value) { if (value < -273.15) throw new RangeError('Below absolute zero'); this.#celsius = value; } get fahrenheit() { return this.#celsius * 9 / 5 + 32; } set fahrenheit(value) { this.celsius = (value - 32) * 5 / 9; } get kelvin() { return this.#celsius + 273.15; } } const t = new Temperature(100); console.log(t.celsius); // 100 console.log(t.fahrenheit); // 212 console.log(t.kelvin); // 373.15 t.fahrenheit = 32; console.log(t.celsius); // 0
Result
Open