Getters and setters to control access to an object

Розкажіть нам, що сталося:
Calling the setter with a Celsius value should set the temperature.
If setting to this.temperature - RangeError: Maximum call stack size exceeded.
If changing ‘get/set temperature’ to something else - A getter should be defined. A setter should be defined.

Ваш код


class Thermostat {
  constructor(fahrenheit) {
    this._temperature = (5 / 9) * (fahrenheit - 32);
  }
  get temperature() {
    return this._temperature;
  }
  set temperature(celsius) {
    this._temperature = (celsius * 9) / (5 + 32);
    }
}

const thermos = new Thermostat(76); // Налаштування у шкалі Фаренгейта
let temp = thermos.temperature; // 24.44 градусів за Цельсієм
thermos.temperature = 26;
temp = thermos.temperature; // 26 градусів за Цельсієм

Інформація про ваш браузер:

Агент користувача: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Завдання: ES6 - Гетери й сетери для управління доступом до об’єкта

Посилання на завдання:

This pair doesn’t go together. Either your temperature is internally stored in C or F. In your getter you are acting like _temperature is C but in your setter you are acting like _temperature is in F.