DEV Community

Randy Rivera
Randy Rivera

Posted on

Use getters and setters to Control Access to an Object

You can obtain values from an object and set the value of a property within an object.

These are called getters and setters.

Getter functions are meant to simply return (get) the value of an object's private variable to the user without the user directly accessing the private variable.

Setter functions are meant to modify (set) the value of an object's private variable based on the value passed into the setter function. This change could involve calculations, or even overwriting the previous value completely.

Let's Challenge ourselves:

  • Use the class keyword to create a Thermostat class. The constructor accepts a Fahrenheit temperature.

  • In the class, create a getter to obtain the temperature in Celsius and a setter to set the temperature in Celsius.

  • Remember that C = 5/9 * (F - 32) and F = C * 9.0 / 5 + 32, where F is the value of temperature in Fahrenheit, and C is the value of the same temperature in Celsius.

// Only change code below this line

// 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
Enter fullscreen mode Exit fullscreen mode
  • Answer:
class Thermostat {
  constructor(fahrenheit) {
    this.fahrenheit = fahrenheit;
  }
  get temperature() {
    return(5 / 9) * (this.fahrenheit - 32);
  }
  set temperature(celsius) {
    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
Enter fullscreen mode Exit fullscreen mode

Top comments (0)