Use getters and setters to Control Access to an Object: Do i understand it right?

Tell us what’s happening:
I solved the challenge, but to be honest after a while i looked at the solution for the last detail.

Now i am trying to understand what is happening there :smiley: So i would describe with my words what is happening there and would be very happy if someone can confirm or correct my “thinking-path”.

const thermos2 = new Thermostat(88);
console.log(thermos2);
console.log(thermos2.temperature)

First we create (i avoid the word set, setting or so to minimize confusion with the code) a object called thermos2 with the property _input and give it the value 88.

The “path” of the code only is through the constructor. It neither touch the getter or the setter, because if i change anything of the formulars, the value will be still 88 :D. It also make sense, because we didnt write anything about temperature to aim for the getter or setter.

If i console.log(thermos2) it will output the object including property and value. Now the metric is in fahrenheit.

But if i console.log(thermos2.temperature), i invoke the getter, which will transform my fahrenheit metric into the celsius. The setter will be untouched.

let temp2 = thermos2.temperature = 23;
console.log(temp2)
console.log(thermos2.temperature)
console.log(thermos2)

Now we create a variable named temp2 and give it the value 23. Because we “just” assigned a random variable a random value, it dont get through the function/class(what would be the better name? i would say function), and is either touched by the getter or setter. You can say it “works” outside of function.

BUT, we also declared, that thermos2.temperature is 23. But this invoke the function, now it went through the getter it return -5. But also went through the setter, which then return again 23. Because the both formula “neutralize” themselve, because they are the same only “upside down” (forgive me mathematicians for my very bad language :smiley: )
Now if we console.log(thermos2.temperature) it will output the celsius 23. But if we console.log(thermos2) it will insert the 23 and now only get through the setter. This transforms my celsiusmetric into the fahrenmetric 73.4. The getter will be untouched.

So finally we could say, to save the fahrenheitdegree we should use Thermostat(d) command and to save the celsiusdegree we should use the thermos2.temperature = d.

And now if we want to show the saved temperature in Fahrenheit we use console.log(thermos2) and to show it in Celsius we should use console.log(thermos2.temperature). Its working in both ways.

Sorry guys for my wall of text and my strange description of what is actually happening. But did i get it right? Or was there a mistake? You dont need to answer with long explanations if its right (i would obvious still appreciate it, but dont want to take to much of your time)

  **Your code so far**
// Only change code below this line
class Thermostat {
constructor(input) {
  this._input = input;
}
//getter
get temperature() {
  return (5/9) * (this._input - 32);
}
//setter
set temperature(updatedInput) {
  this._input = updatedInput * 9 / 5 + 32;
}
}
// 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

const thermos2 = new Thermostat(88);

console.log(thermos2);
console.log(thermos2.temperature)

let temp2 = thermos2.temperature = 23;
console.log(temp2)
console.log(thermos2.temperature)
console.log(thermos2)

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Use getters and setters to Control Access to an Object

Link to the challenge:

This is not a good idea. Each assignment should be on its own line.