ADVANCED OBJECTS: Getters

PHOTO EMBED

Tue Oct 05 2021 16:09:40 GMT+0000 (Coordinated Universal Time)

Saved by @ianvalentino #javascript #objects #getters #advancedobjects

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: cannot retrieve energy level'
    }
  }
};

console.log(robot.energyLevel)
content_copyCOPY

https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/getters