_
). However, the practice itself does not make a variable private.
class
keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.
Now create a getter
and a setter
in the class, to obtain the temperature in Celsius.
Remember that C = 5/9 * (F - 32)
and F = C * 9.0 / 5 + 32
, where F
is the value of temperature in Fahrenheit, and C
is the value of the same temperature in Celsius.
Note: When you implement this, you will track the temperature inside the class in one scale, either Fahrenheit or Celsius.
This is the power of a getter and a setter. You are creating an API for another user, who can get the correct result regardless of which one you track.
In other words, you are abstracting implementation details from the user.
Thermostat
should be a class
with a defined constructor
method.
testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function');
- text: class
keyword should be used.
testString: assert(code.match(/class/g));
- text: Thermostat
should be able to be instantiated.
testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
- text: A getter
should be defined.
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.get === 'function';})());
- text: A setter
should be defined.
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.set === 'function';})());
- text: Calling the setter
should set the temperature.
testString: assert((() => {const t = new Thermostat(32); t.temperature = 26;return t.temperature !== 0;})());
```