实现说明
Temperature承担全部实现。
顶层就这几处状态:temperature、descriptor。
使用要点
- 跑完文件,输出里会留下标记——
celsius:、fahrenheit:、after boiling point:。
常见问题
除了语言运行时还需要什么准备?
什么都不需要——语言运行时是唯一要求。 文件的状态由几个模块级绑定持有:temperature、descriptor。
核心语句
return (this.celsius * 9) / 5 + 32;
代码示例
class Temperature {
accessor celsius = 0;
get fahrenheit(): number {
return (this.celsius * 9) / 5 + 32;
}
set fahrenheit(value: number) {
this.celsius = ((value - 32) * 5) / 9;
}
}
const temperature = new Temperature();
temperature.celsius = 25;
console.log("celsius:", temperature.celsius);
console.log("fahrenheit:", temperature.fahrenheit);
temperature.fahrenheit = 212;
console.log("after boiling point:", temperature.celsius);
const descriptor = Object.getOwnPropertyDescriptor(
Temperature.prototype,
"celsius",
);
console.log("has getter:", typeof descriptor?.get === "function");
console.log("has setter:", typeof descriptor?.set === "function");