Advertisement
makispaiktis

Setters and getters

Oct 20th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const robot = {
  2.   _model: '1E78V2',
  3.   _energyLevel: 100,
  4.   _numOfSensors: 15,
  5.   get numOfSensors(){
  6.     if(typeof this._numOfSensors === 'number'){
  7.       return this._numOfSensors;
  8.     } else {
  9.       return 'Sensors are currently down.'
  10.     }
  11.   },
  12.   set numOfSensors(num){
  13.       if(typeof num === 'number' && num >= 0){
  14.           this._numOfSensors = num;
  15.       }
  16.       else{
  17.           console.log('Pass in a number that is greater than or equal to 0');
  18.       }
  19.   }
  20. };
  21.  
  22. robot.numOfSensors = 100;
  23. console.log(robot.numOfSensors);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement