Use getters and setters to Control Access to an Object

Use getters and setters to Control Access to an Object
0.0 0

#1

Tell us what’s happening:

I got this right but I still don’t quite understand. When making temp = thermos.temperature;. The new Object thermos doesn’t have a temperature property.

If it was something like thermos.temp it would make more senses, right?. What am I missing something?

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor(F) {
    this.temp = F;
    }
    get celsius(){
      return 5/9 * (F - 32);
    }

    set celsius(C){
      this.temp = C;
    }
  }
  /* 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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object


#2

It looks like the exercise has too few tests in place. I can pass it with just this:

  /* Alter code below this line */
  class Thermostat {}
  /* Alter code above this line */

I think the intention is for you to make a temperature getter and setter, since that’s what the code at the bottom is using.


#3

Same, I can basically bypass the test by putting in,

/* Alter code below this line /
class Thermostat {
constructor(Fahrenheit){
this.Thermostat=temp;
}
}
/
Alter code above this line */


#4

This is the right way to access the value.