ES6: Use getters and setters to Control Access to an Object - pulkitbhutani/My-FreeCodeCamp-Code GitHub Wiki

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

Now create getter and setter in the class, to obtain the temperature in Celsius scale.

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

Note

When you implement this, you would be tracking the temperature inside the class in one scale - either Fahrenheit or Celsius.

This is the power of getter or setter - you are creating an API for another user, who would get the correct result, no matter which one you track.

In other words, you are abstracting implementation details from the consumer.

My explanation -

getter and setter solve the problem of abstraction from javascript and gives and easy way to retrieve details from an object just like an API, so we can modify the privates also.

Getter - In the below example, getter is caller with - let temp = thermos.temperature; constructor will fill in the inital value to fahrenheit and then get will do the job of converting it to celsius.

Setter- Set is used to change the initial value which has been given to fahrenheit during object creation, we can modify the value using it.

Solution -

function makeClass() {
  "use strict";
  /* Alter code below this line */
class Thermostat{
  constructor(fahrenheit)
  {
    this._fahrenheit = fahrenheit;
  }

  get temperature(){
    return ((5/9)*(this._fahrenheit - 32));
  }
  set temperature(newTemp){
    this._fahrenheit=newTemp;
  }
}
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C