ES6 - Use getters and setters to Control Access to an Object

Am I correct in that, what the getter function returns–in this case, Celsius calculated–is always passed directly to the setter function as an argument…and that, again, in this case we create a celsius parameter name to use that argument?

class Thermostat {
  constructor(fahrenheit) {
    this._fahrenheit = fahrenheit;
  }
  get temperature() {
    return (5 / 9) * (this._fahrenheit - 32);
  }
  set temperature(celsius) {
    return this._fahrenheit = (celsius * 9.0) / 5 + 32;
  }
}

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 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0

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

Link to the challenge:

I’m not sure what you mean here. Can you clarify?

Calling the getter does not involve the setter and vice-versa. They are independent methods. The setter takes Celsius as an argument because that’s the requirement for this challenge. It could take Fahrenheit instead.

Hi again,

Thanks for the response. I’ll try to clarify.

In this code challenge…

  • get temperature( ) evaluates and returns a celsius temperature value.

  • set temperature( celsius ) uses the parameter “celsius”, presumably to pass the value returned from get temperature( ) to the expression ( celsius * 9.0 ) / 5 + 32; in the set temperature function.

My question is, I suppose. In situations, inside a class constructor(or maybe even elsewhere) do the get and set functions always work in conjunction like this. get temperature( ) clearly passes its value into the parameter for set temperature ( ).

Also, if more than one value is returned from get xfunction( ) does the get xfunction(a, b, c ) accept multiple parameters. I’d assume it would.

get temperature() {
    return (5 / 9) * (this._fahrenheit - 32);
  }
  set temperature(celsius) {
    return this._fahrenheit = (celsius * 9.0) / 5 + 32;
  }

The setter and getter are independent methods. Invoking one does not call the other. Your setter should not have an explicit return statement because a setter does not return a value, it only sets a value ( this._fahrenheit in this case). Your setter still works because the value returned is just being ignored. But you can get rid of the word return in the setter and it will still work correctly.

This calls the setter:

thermos.temperature = 26;

The only thing that happens when this line is executed is that this._fahrenheit is set to the value 26 converted to Fahrenheit.

Likewise, this calls the getter:

temp = thermos.temperature;

The only thing that happens is that this._fahrenheit is converted to Fahrenheit and then returned to from the getter (and in this case that value is assigned to temp).

Both of these methods are utilizing the class property this._fahrenheit but they are not calling each other.

Can you explain where you are seeing this?

Getters don’t accept arguments.

MDN: get

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