Getters and Setters in ES6

Tell us what’s happening:

Well it’s giving me a error “RangeError: Maximum call stack size exceeded”, and some advices from freeCodeCamp:
image

Somebody can help me?

Your code so far



class Thermostat{
constructor(temperature){
  this.temperature = temperature;
}
// setter
set temperature(celsius){
  this.temperature = (celsius * 9) / 5 + 32;
}
// getter
get temperature(){
  return (this.temperature - 32) * 5/9;
} 
}
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/81.0.4044.138 Safari/537.36.

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

Link to the challenge:

1 Like

That is strange. Have you tried clearing your cache?

Tried now, and the errors still the same. Tried in another browser and didn’t worked too.

@luizedc1

Follow the example code, hope, you will get your solution there.

Are you missing an underscore something like this._example=something .

Use it wherever necessary as mentioned in the example code in your challenge .

1 Like

Oh, that worked, thank you!! But I don’t understand why it don’t work without the underscore…

1 Like

@luizedc1
You are working with es6 challenges and this is es6 syntax to declare private variable. Different program has different syntax, you can learn more about this by searching the topic

1 Like