_
)。这里并没有真正意义上让变量私有。
class
关键字来创建Thermostat
类,它的构造函数应该可以接收 fahrenheit(华氏温度)作为参数。
在类中创建 temperature 的 getter
和setter
,将温度转换成摄氏温度。
温度转换的公式是C = 5/9 * (F - 32)
以及F = C * 9.0 / 5 + 32
,F 代表华氏温度,C 代表摄氏温度。
注意: 当你实现这个作业的时候,你应当在类中使用一个温度标准,无论是华氏温度还是摄氏温度。
是时候展现 getter 和 setter 的威力了——无论你的 API 内部使用的是哪种温度标准,用户都能得到正确的结果。
或者说,你从用户需求中抽象出了实现细节。
Thermostat
应该是一个class
,并且在其中定义了constructor
方法。
testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function');
- text: 应该使用 class
关键字。
testString: assert(code.match(/class/g));
- text: Thermostat
应该可以被实例化。
testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
- text: 应该定义一个 getter
。
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.get === 'function';})());
- text: 应该定义一个 setter
。
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.set === 'function';})());
- text: 调用 setter
应该设置 temperature。
testString: assert((() => {const t = new Thermostat(32); t.temperature = 26;return t.temperature !== 0;})());
```