Tell us what’s happening:
I have 2 code like this, I still don’t understand why they give different results when the console comes to the screen. Can someone explain to me the difference between the 2 code below? Thanks
Your code so far
//THE FIRST CASE
function makeClass() {
"use strict";
class Thermostat{
constructor(farenheit){
this.farenheit = farenheit;
}
get temperature(){
return 5 / 9 * (this.farenheit - 32);
}
set temperature(celsius){
this.farenheit = celsius * 9.0 / 5 + 32;
}
}
return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature;
console.log(temp); // 24.44
thermos.temperature = 26;
console.log(temp); // 24.44
// THE SECOND CASE
function makeClass() {
"use strict";
class Thermostat{
constructor(farenheit){
this.farenheit = farenheit;
}
get temperature(){
return 5 / 9 * (this.farenheit - 32);
}
set temperature(celsius){
this.farenheit = celsius * 9.0 / 5 + 32;
}
}
return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature;
console.log(temp); // 24.44
thermos.temperature = 26;
console.log(thermos.temperature); // 26
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/77.0.126 Chrome/71.0.3578.126 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/