Tell us what’s happening:
I’m not fully understanding what is happening in this challenge’s solution code and wanted to get an explanation line by line. I’ll try to explain what I’m understanding… (I got it to pass but heavily relied on peeping the solution, as my lack of understanding will probably show)
**Your code so far**
// Only change code below this line
class Thermostat {
constructor(fahr) {
this.fahr = fahr;
}
get temperature() {
return (5 / 9) * (this.fahr - 32);
}
set temperature(celsius) {
this.fahr = (celsius * 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
so starting with the line
const thermos = new Thermostat(76); // Setting in Fahrenheit scale
this declares a variable thermos
that includes an object (or is thermos the object?) instantiated by the new
keyword, and the object contains one property with a value: {fahr: 76}
(I don’t know if any of this is right)
Then comes:
let temp = thermos.temperature;
this declares a variable temp
which equals value of the thermos
object’s property temperature
But I don’t think thermos.temperature
has been defined/declared yet, so does this ‘call’ the get
portion of code so that it can determine the value of temperature
based on the value of the thermos.fahr
property?
Also does the this
keyword in this.fahr
just reference the fahr
property of any objects with the class Thermostat
?
And then I’m not even sure what the purpose of the next 2 lines is:
thermos.temperature = 26;
temp = thermos.temperature;
This just seems like it’s changing the value of the temp
variable, thus changing the corresponding value of the thermos.temperature
property
I’m blindly guessing that by changing this value, either the get or set portion of code would then update the corresponding thermos.fahr
property’s value to whatever 26 celsius is in fahrenheit?
Sorry that was long winded but I’ve been on this challenge all day and my brain is now oatmeal
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36
Challenge: Use getters and setters to Control Access to an Object
Link to the challenge: