Don't we need to check `_numOfSensors` instead?

Either way will work, but using the setter method allows you to make sure that the property is set in a way that you intend.

With the numOfSensors property, if you set it directly using _numOfSensors, you can set it to anything - a string, Null, false, undefined, another object - there’s nothing to flag it as an error. By making a set numOfSensors method, you can check the input to make sure that it is a valid number before assigning it to _numOfSensors.

For example, you may have other code that depends on the _numOfSensors property, and maybe if _numOfSensors is accidentaly set to a string instead of a number that code will not work properly. If you give a setter method it’s less likely that the _numOfSensors property will be set to an invalid value, since the set method would catch the error.

1 Like

What do you mean by "we’re only polling the variable for its current value." ? If we assign a different name to the setter here, will it not be less confusing? and why does the value of _numOfSensors (i,e 15) also get logged in when i assign a string to the setter?
Here is the code, please elaborate
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 sensors(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.sensors = ‘sdf’;

console.log(robot._numOfSensors);
Output: Pass in a number that is greater than or equal to 0
15

Hi, I know that I am new to the conversation here. Please correct me if I’m wrong in my understanding of getters and setters. Is it right to say that getters essentially help to call an object faster than just say making a function and using dot notation? And that setters change the original value of the property of that object if so needed? Just looking for the simplest, most direct answer. Thanks.

By conventional means we access and write object properties that exist using dot notation.

console.log(object.prop)
object.prop = 'value'

Getters operate with the same syntax but offer an additional layer of abstraction where we can process the data from its original source without altering the data. The data could be secured as one type and accessed as another, if so desired.

Setters can operate along the same lines. They use conventional syntax but have a layer of abstraction that can validate and/or modify type for the purpose of storing. What we do with a setter does alter the original, but only according to the set rules this software layer affords us.

One suspects it is possible to even validate instance parameters through clever use of the setter, though pure speculation, mind. Accessors are a part of OOP and even if they are more decoration in JS than anything fundamental, it bears learning how to incorporate them into one’s code architecture for the sake of future friendliness, and to develop habits that easily flow into other OOP languages.