The problem states that the setter function should “set the temperature in Celcius” but the provided answer sets ‘this.fahrenheit’ = Celcius to Fahrenheit conversion. What is the setter function here actually accomplishing? I understand what it’s doing in the Book constructor example, but I am having trouble wrapping my head around the setter function. Are getters and setters mutually exclusive from each other?
**Your code so far**
// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
this.fahrenheit = fahrenheit;
}
// getter
get temperature() {
return (5/9) * (fahrenheit - 32);
}
// setter
set temperature(celcius) {
}
}
// 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 Edg/100.0.1185.44
Challenge: Use getters and setters to Control Access to an Object
but I am having trouble wrapping my head around the setter function
No, not really.
Imagine you are a programmer at a bank and have to create a class for accounts and you want to access how people access a certain piece of data. Say you create a class, BankAccount. You have a property “balance”. You don’t want anyone to be able to access that - you need to make sure they have permission. And you need to check what their currency is (imagining this is an international account). And you don’t want anyone to change that balance willy-nilly - you need to make sure they have permission, that the funds have cleared, the proper records are generated, etc.
So, you create a private property called “_balance” (the underscore is a common convention) that only the class is allowed to access. Then in some languages you would create methods like getBalance and setBalance. You would have complete control over that balance. A programmer, even by accident, couldn’t come in and use your module and start messing around with the balance - they can only do it in ways that the getter and setter allow. Does that make sense why that is a good thing? Now you can publish that module, but you are still controlling how that data is used and changed
Like I said, in some languages you would create methods like getBalance and setBalance. JS went a different direction. They just let you define “get balance()” and “set balance()” and outside the module you will treat them like normal properties. But the class will know how to handle them.
Are getters and setters mutually exclusive from each other?
No, typically you are going to have both. But you could have just one. It really depends on what access you want to give outside the module. Maybe you want them to be able to read the data but not change it. Maybe you want them to be able to change it but not read it.
Does that all make sense? Yeah, it’s kind of a silly little example, but that is the nature of these things.
Looking at your code, there is an issue with this line:
return (5/9) * (fahrenheit - 32);
and you need to define your setter. Where is that value stored? How do you take that parameter and save the data correctly?
Thank you for explanation! What I took away from it was that access to the balance property is abstracted away from the developer and user so it can’t be modified accidentally or intentionally.
My confusion was with the setter function and problem description. The description states I need to create a setter function to set the temperature in Celsius, but the implementation in the solution looks like it’s converting Celsius to Fahrenheit. Am I misunderstanding the context here? Why is the setter function setting this.fahrenheit = Fahrenheit conversion?
I mean, the user can’t change it anyway, unless by customer you mean another developer that is using your module.
The description states I need to create a setter function to set the temperature in Celsius, but the implementation in the solution looks like it’s converting Celsius to Fahrenheit.
Yes, it is a strange little problem. And a strange little description. But then again, that is all too common in the dev world.
This is how I read it:
The constructor accepts a Fahrenheit temperature.
When you construct the class instance, you will pass in a temp in Fahrenheit.
In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.
So, when you deal with the getter and setter, you will deal with Celsius.
Why create with F and then deal with C? Who knows? But there will be things you have to code that won’t make sense sometimes. Just yesterday at work there was something I disagreed with everyone on. I think they’re all nuts, that they want this feature to work a certain way. I think it’s nuts. But sometimes that’s the way it is.