const robot = {
_model: '1E78V2',
_energyLevel: 100,
get energyLevel () {
if (typeof this._energyLevel === 'number') {
return `My current energy level is ${this._energyLevel}`;
} else {
return 'System malfunction: can not retrieve energy level';
}
}
};
console.log(robot.energyLevel);
The code above is what I have after completing the getters exercise in the getters lesson of Intro to JavaScript Advanced Objects, but I had two quick questions about it.
First, when we say get energyLevel()
to make the function, why do we say energyLevel
and not _energyLevel
with the underscore to match property above?
Additionally, why don’t we say robot._energyLevel
in the console.log
statement at the bottom?