본문 바로가기

JAVASCRIPT

codecademy 객체 예제

const robot = {
  _model: '1E78V2',
  _energyLevel: 100,
  _numOfSensors: 15,
  get numOfSensors(){
    if(typeof this._numOfSensors === 'number'){
      return this._numOfSensors;
    } else {
      return 'Sensors are currently down.'
    }
  },
  set numOfSensors(num) {
    if (typeof num === 'number' && num >= 0){
      this._numOfSensors = num;
    } else {
      console.log('Pass in a number that is greater than or equal to 0')
    }   
  } 
};

robot.numOfSensors = 100;
console.log(robot.numOfSensors);

'JAVASCRIPT' 카테고리의 다른 글

code academy object 예제3  (0) 2020.07.09
code academy object 예제2  (0) 2020.07.09
자바스크립트 es6 $ 기능 ` 의 기능  (0) 2020.07.07
드림코딩 자바스크립트 첫번째  (0) 2020.07.07
OBJECTS Pass By Reference  (0) 2020.06.25