I think I fundamentally don't understand the getters and setters lesson

I’ve already finished the lesson but I just can’t move on from it completely yet because I didn’t really understand everything about it completely. First, I wasn’t really sure what the code’s output or purpose was supposed to be, I think it was supposed to convert between Celsius and Fahrenheit while still being able to keep both values? Here is my code, and I’ve put most my questions inside it through comments.

`// Only change code below this line
class Thermostat {
  //constructor creates an empty object with "this", and then puts a "tempF" property inside it with the value of the "tempF" initialized with the constructor's parameter
  constructor(tempF) {
    this.tempF = tempF; //this part of the code receives a temperature in fahrenheit
  get temperature() {
    return (5 / 9) * (this.tempF - 32); //this part of the code gets the fahrenheit temp value received from the constructor, and returns it as celsius, but doesnt the thermoStat class always return the value in terms of fahrenheit anyway? where does this celsius value come in?
  set temperature(tempC) {
    this.tempF = (tempC * 9.0) / 5 + 32; //i really dont understand this part of the code; what exactly is it doing? from what i know, it converts the current "tempF" property to celsius, and then sets that as the new value? but i never see that celsius value anywhere in the output since Thermostat always returns a value in terms of fahrenheit

// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale

let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
//what exactly do these three lines of code (19-21) do? line 19 says "24.44 in Celsius", which i assume is converted from 76F. the conversion happened because "thermos.temperature" calls "get temperature", which has a block of code that returns a fahrenheit value in celsius.
//then line 20 happens, which sets the "thermos.temperature" value to 26. can i assume the the line of code "thermos.temperature = 26" calls the set function, because the 26 in "thermos.temperature = 26" is now tempC?
//if yes, does that mean "thermos.temperature = 26" has the same function as "thermos.temperature(26)" (which from what i know is how you call a function so its like the typical <function name>(<parameter>)?
//if not, then how does the function know that 26 is in celsius? i know that it auto-converts it to fahrenheit since the constructor accepts a fahrenheit temp

console.log(thermos); //it displays a tempF value like i said earlier, where does the celsius value come in?`

I’ll be really grateful if you can explain this to me, thanks!

Lines at the end are to show how this class is supposed to be used. Class is instantiated with temperature with Fahrenheit, temperature can set and return temperature in Celsius.

Following your implementation:

  • temperature is kept internally as Fahrenheit
  • getter calculates temperature in Celsius, from the kept Fahrenheit and returns it
  • setter receives temperature in Celsius, calculates from it temperature in Fahrenheit and saves it internally

Notice now, from the outside of class it doesn’t matter in which scale is temperature kept internally. With some changes it could be kept in Celsius, Kelvin or any other scale. As long as getter, setter and constructor fulfills initial requirements - class is instantiated with Fahrenheit, temperature sets and gets temperature in Celsius - it doesn’t matter what is happening inside of class.

Ok, I think I finally understand it! So basically the class can receive any value without specifying whether it’s in Fahrenheit or Celsius, since the class itself can differentiate between the two depending on how you put in the values.

So for example, in const thermos = new Thermostat(76) , the value 76 is in Fahrenheit since it’s put directly into the class Thermostat, meanwhile in thermos.temperature = 26 and its accompanying code, the value 26 is in Celsius since temperature is directly called, which is the one responsible for holding the values in Celsius.

Did I understand it correctly?

Most of it I believe. In your implementation class is not holding values in Celsius per se, it’s calculated when needed, when getting or setting temperature.

Different implementation could use Celsius for internal keeping - when constructed Fahrenheit would need to be converted to Celsius, and temperature setter and getter could use that internally kept Celsius directly.

Both of these from the outside work the same, just internals are different.


Kind of a confusing class to be honest. You wouldn’t create something like this in practice but it helps demonstrate how getters and setters work.

Basically when create an instance of this class it expects the value to be in Fahrenheit, which is then stored in this.tempF.

When you “get” the value with thermos.temperature the class converts the Fahrenheit value to Celcius.

When you “set” the value with thrmos.temerature = 26 it’s expecting the value to be Celcius but converts the value to Fahrenheit internally.

Internally the class is always storing the value as Fahrenheit .

Hopefully that helps clarify it a bit :slight_smile:

The lesson is trying to show you how you might have some logic associated with getting and setting a value so you understand why you might want to use getters and setters.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.