4.1 KiB
4.1 KiB
id, challengeType, forumTopicId, title
id | challengeType | forumTopicId | title |
---|---|---|---|
587d7b8c367417b2b2512b54 | 1 | 301220 | 使用 getter 和 setter 来控制对象的访问 |
Description
class Book {
constructor(author) {
this._author = author;
}
// getter
get writer() {
return this._author;
}
// setter
set writer(updatedAuthor) {
this._author = updatedAuthor;
}
}
const lol = new Book('anonymous');
console.log(lol.writer); // anonymous
lol.writer = 'wut';
console.log(lol.writer); // wut
注意我们调用 getter 和 setter 的语法,它们看起来并不像一个函数调用。 Getter 和 Setter 非常重要,因为它们隐藏了内部的实现细节。
注意: 通常会在私有变量前添加下划线(_
)。这里并没有真正意义上让变量私有。
Instructions
class
关键字来创建Thermostat
类,它的构造函数应该可以接收 fahrenheit(华氏温度)作为参数。
在类中创建 temperature 的 getter
和setter
,将温度转换成摄氏温度。
温度转换的公式是C = 5/9 * (F - 32)
以及F = C * 9.0 / 5 + 32
,F 代表华氏温度,C 代表摄氏温度。
注意: 当你实现这个作业的时候,你应当在类中使用一个温度标准,无论是华氏温度还是摄氏温度。
是时候展现 getter 和 setter 的威力了——无论你的 API 内部使用的是哪种温度标准,用户都能得到正确的结果。
或者说,你从用户需求中抽象出了实现细节。
Tests
tests:
- text: <code>Thermostat</code>应该是一个<code>class</code>,并且在其中定义了<code>constructor</code>方法。
testString: assert(typeof Thermostat === 'function' && typeof Thermostat.constructor === 'function');
- text: 应该使用 <code>class</code> 关键字。
testString: assert(code.match(/class/g));
- text: <code>Thermostat</code>应该可以被实例化。
testString: assert((() => {const t = new Thermostat(32);return typeof t === 'object' && t.temperature === 0;})());
- text: 应该定义一个 <code>getter</code>。
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.get === 'function';})());
- text: 应该定义一个 <code>setter</code>。
testString: assert((() => {const desc = Object.getOwnPropertyDescriptor(Thermostat.prototype, 'temperature');return !!desc && typeof desc.set === 'function';})());
- text: 调用 <code>setter</code> 应该设置 temperature。
testString: assert((() => {const t = new Thermostat(32); t.temperature = 26;return t.temperature !== 0;})());
Challenge Seed
/* Alter code below this line */
/* Alter code above this line */
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C
Solution
/* Alter code below this line */
class Thermostat {
constructor(fahrenheit) {
this._tempInCelsius = 5/9 * (fahrenheit - 32);
}
get temperature(){
return this._tempInCelsius;
}
set temperature(newTemp){
this._tempInCelsius = newTemp;
}
}
/* Alter code above this line */
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C