I am having problems with: Use getters and setters to Control Access to an Object
I am writing code in VSC and running chrome dev tools to look at results, and at this moment after running first few tests:
const thermos = new Thermostat(76); // Setting in Fahrenheit scale
I can see that I create an object that has two properties:
variable farhn = 76 and a function celsius that returns “24.44” (btw why is it in quotes?) and the property temperature with its value of from setter is missing.
And the exercise tests tell me that I also failed to define my getter and setter…
**Your code so far**
// Only change code below this line
class Thermostat {
constructor(farhn){
this.farhn = farhn;
}
get celsius() {
return ((this.farhn - 32) * 5 / 9).toFixed(2);
}
set celsius(cels) {
this.temperature = cels;
}
}
// 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
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Challenge: Use getters and setters to Control Access to an Object
The setter method also needs to do the conversion of the input value to Fahrenheit before setting the value. Coz the object stores the temperature in Fahrenheit internally and to the user it appears as if the implementation is in Celsius.
That is what is the main concept of using getter and setter methods here as properties in disguise - to hide the underlying implementation from the external user. And also to keep the actual data hidden so that it is not directly accessible without going through the getter and setter methods which give an additional layer of security.
Also use giving meaningful variable names like this._temperature would help coz here we are getting and setting the temperature. F and C are just units of measurement. Also precede the variable name with an underscore to indicate that it is a hidden variable.
Hint 4:
Create a set method of the same name as the get method. It should have a parameter that accepts celsius temperature. Convert it to fahrenheit, and set it to the attribute.
at what part did the exercise asked us to covert anything back??
Looking at your code, you’ve created a getter and setter for a property called “celsius”. In the test code, it makes it clear that the property supposed to be called “temperature”:
// ...
let temp = thermos.temperature; // 24.44 in Celsius
// ...
temp = thermos.temperature; // 26 in Celsius
// at what part did the exercise asked us to covert anything back??
It doesn’t matter when you convert. The constructor takes F, and the setter takes C and the getter returns C.
Also, this part is correct but please remove the toFixed(2) from here as it might coz trouble in passing the test. Coz the test is not looking for that.
Yes it is not mentioned explicitly in the exercise but looks like it expects that - abstraction from the user. The instance of the class keeps the temperature within itself in one unit while in the outside it is shown and taken in another unit.
Yeah I thought that it is just a “function name” so my function name was “celsius” and it tried to store a parameter “temperature”.
Where I failed was even after reading provided theory I managed to look at:
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
and think that it was dot notation for an object, while it was invoking function called temperature,
Yeah, in some languages, getters and setters are treated like functions. You define “getTemperature” and you would access it as thermos.getTemperature. But in JS, you define it with the keyword “get” and then access it as thermos.temperature - JS will know from context if you want the setter or getter. You give the internal property a different name, a “secret” name. It doesn’t really matter what you call it hear, but a common convention is to precede it with an underscore, like “_temperature” to let devs know that that is an internal property and is not meant to be accessed outside the class (in most languages and in modern JS you can enforce that.)