Please refer to chapter " ES6: Use getters and setters to Control Access to an Object"
-
Why is class defined under a function ?
-
I didn’t understand
return Thermostat;
statement after class definition ? -
If I comment out code which is related to function then also it works fine, then which case/s I should definite class within a function. This concept is super confusing for Java engineer like me
function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat{
constructor(tempInFahrenheit){
this._tempInFahrenheit = tempInFahrenheit;
}
get temperature(){
return 5/9 * ( this._tempInFahrenheit - 32 );
}
set temperature(tempInCelcius){
this._tempInFahrenheit = tempInCelcius * 9.0 /5 +32;
}
}
/* 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