请向我解释一下 JavaScript 中的 setter 如何在类中工作

发布于 2025-01-15 07:39:53 字数 1284 浏览 1 评论 0原文

目标:使用 class 关键字创建 Thermostat 类。构造函数接受华氏温度。在该类中,创建一个 getter 来获取摄氏温度,并创建一个 setter 来设置摄氏温度。

到目前为止,这是我的代码:

class Thermostat {
  constructor(fahrenheit){
    this.fahrenheit = fahrenheit;
  }

  get temperature(){
    const inCelcius = 5/9 * (this.fahrenheit - 32) ;
    return inCelcius;

  }

  set temperature (temperatureInCelcius){
    this.fahrenheit = temperatureInCelcius;
  }

}

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // should show 24.44 in Celsius
console.log(temp);
thermos.temperature = 26;
temp = thermos.temperature; // should show 26 in Celsius but right now showing -3.333
console.log(temp);

我知道正确的解决方案是将 this.fahrenheit = temperatureInCelcius; 更改为 this.fahrenheit = (celsius * 9.0) / 5 + 32;

但是我不明白为什么。我被告知我的代码缺少转换。

我的问题是:

  1. 为什么需要将华氏度转换为摄氏度?
  2. 这一行到底发生了什么? thermos.Temperature = 26; 我的理解是,当编译器看到该行时,它会在构造函数内调用该行
set temperature (temperatureInCelcius){
    this.fahrenheit = temperatureInCelcius;
  }

,然后将 26 放入参数 (temperingInCelcius) 中,然后将 this.fahrenheit 的属性值设置为 26。

只是想要一些解释,因为感觉好像我遗漏了一些东西。非常感谢您的帮助! <3

The Goal: Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature. In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.

Here's my code so far:

class Thermostat {
  constructor(fahrenheit){
    this.fahrenheit = fahrenheit;
  }

  get temperature(){
    const inCelcius = 5/9 * (this.fahrenheit - 32) ;
    return inCelcius;

  }

  set temperature (temperatureInCelcius){
    this.fahrenheit = temperatureInCelcius;
  }

}

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // should show 24.44 in Celsius
console.log(temp);
thermos.temperature = 26;
temp = thermos.temperature; // should show 26 in Celsius but right now showing -3.333
console.log(temp);

I know the correct solution is to change this.fahrenheit = temperatureInCelcius; into this.fahrenheit = (celsius * 9.0) / 5 + 32;

But I don’t understand why. I've been told that my code is missing a conversion.

My questions are:

  1. Why is there a need to convert Fahrenheit into celsius?
  2. What exactly happens in this line? thermos.temperature = 26; My understanding is that when the compiler sees that line, it invokes this line inside the constructor
set temperature (temperatureInCelcius){
    this.fahrenheit = temperatureInCelcius;
  }

and then put 26 into the argument (temperatureInCelcius), then set the property value of this.fahrenheit to 26.

Just want some explanation cause it feels like I am missing something. Would greatly appreciate your help! <3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别低头,皇冠会掉 2025-01-22 07:39:53

当您执行 temp = Thermos.Temperature; 时,会调用 getTemperature() 并为 thermos.Temperature = 26; 设置温度() 被调用。

// thermos.temperature = 76
const thermos = new Thermostat(76);

// this calls get temperature() and return 24.44
// 5/9 * (76 - 32) = 24.44
let temp = thermos.temperature;

console.log(temp); // 24.44

// this calls set temperature() and sets thermos.temperature = 26
thermos.temperature = 26;

// this calls get temperature() and return -3.333
// 5/9 * (26 - 32) = -3.333
temp = thermos.temperature;

console.log(temp); // -3.33

When you do temp = thermos.temperature;, get temperature() is called and for thermos.temperature = 26; set temperature() is called.

// thermos.temperature = 76
const thermos = new Thermostat(76);

// this calls get temperature() and return 24.44
// 5/9 * (76 - 32) = 24.44
let temp = thermos.temperature;

console.log(temp); // 24.44

// this calls set temperature() and sets thermos.temperature = 26
thermos.temperature = 26;

// this calls get temperature() and return -3.333
// 5/9 * (26 - 32) = -3.333
temp = thermos.temperature;

console.log(temp); // -3.33
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文