Underscore naming convention


#1

I understand that the underscore is naming convention, but when I remove the keywords “get” and “set” on the solution to the exercise, the code still runs with the same result. Are the words “get” and “set” also naming convention?


Why do I need a setter, if I am using _ to not have the property changed by others?
#2

Much more than naming convention…

Consider,

> robot = {
     _model: '1E78V2',
     _energyLevel: 100,
     get energyLevel () {
       return this._energyLevel;
     },
     set energyLevel(numLevel) {
       if( typeof numLevel === "number" && numLevel > 0 ){
         this._energyLevel = numLevel;
       } else {
         console.log( 'Error:  Value is not a number, or not greater than zero' );
      }
     }  
   }
<- {_model: "1E78V2", _energyLevel: 100}
 > robot.model
<- undefined
 > robot.energyLevel
<- 100
 > 

And now with get and set removed,

 > robot = null
<- null
 > robot = {
     _model: '1E78V2',
     _energyLevel: 100,
     energyLevel () {
       return this._energyLevel;
     },
     energyLevel(numLevel) {
       if( typeof numLevel === "number" && numLevel > 0 ){
         this._energyLevel = numLevel;
       } else {
         console.log( 'Error:  Value is not a number, or not greater than zero' );
      }
     }  
   }
<- {_model: "1E78V2", _energyLevel: 100, energyLevel: ƒ}
 > robot.model
<- undefined
 > robot.energyLevel
<- ƒ energyLevel(numLevel) {
       if( typeof numLevel === "Number" && numLevel > 0 ){
         this._energyLevel = numLevel;
       } else {
         console.log( 'Error:  Value is not a number, or not greater than zero' );
      }
     }  
 > 

By removing the keywords there remain two methods by the same name so the latter is the one defined, and the former overwritten. That means no getter. Now let’s see if the method acts like a setter…

 > robot.energyLevel = 'five'
<- "five"
 > robot.energyLevel
<- "five"

We have literally overwritten the method that was supposed to set the property.

 > robot._energyLevel
<- 100

#3

Ok so I’ll be sure to not create two different methods with the same name. I’m still not going to use “get” or “set”


#4

Without a quantified reason, the above makes no sense. How does one expect to gain deeper understanding if you simply decide to turn your back on an important concept?

robot = {
     model: '1E78V2',
     _energyLevel: 100,
     get energyLevel () {
       return this._energyLevel;
     },
     set energyLevel(numLevel) {
       if( typeof numLevel === "number" && numLevel > 0 ){
         this._energyLevel = numLevel;
       } else {
         console.log( 'Error:  Value is not a number, or not greater than zero' );
      }
     }  
   }
<- {_model: "1E78V2", _energyLevel: 100}
 > robot.energyLevel = 200
<- 200
 > robot.energyLevel
<- 200
 > robot.energyLevel = 'three'
Error:  Value is not a number, or not greater than zero
<- "three"
 > 

Witness the important validation work the setter can perform. Not so easy in plain object property syntax. We can take this one step further in a class by validating instance parameters when the object instance is created.

 > class Robot {
     constructor (model, energyLevel) {
       this.model = model;
       this.energyLevel = energyLevel;
     }
     get energyLevel () {
       return this._energyLevel;
     }
     set energyLevel(numLevel) {
       if( typeof numLevel === "number" && numLevel > 0 ){
         this._energyLevel = numLevel;
       } else {
         console.log( 'Error:  Value is not a number, or not greater than zero' );
       }
     }
   }
<- undefined
 > robot = new Robot('1E78V2', 'five')
   Error:  Value is not a number, or not greater than zero
<- > Robot {model: "1E78V2"}
 > robot
<- Robot {model: "1E78V2"}
     model: "1E78V2"
     energyLevel: undefined
 > robot.energyLevel = 100
<- Robot {model: "1E78V2", _energyLevel: 100}
     model: "1E78V2"
     _energyLevel: 100
     energyLevel: 100
 > 

#5

Okie dokie let me see if I understand you. My understanding is this:

If I have an object with some property that I would like to access or set, I can create a method called getProperty() and a method called setProperty(parameters)

then (object.getProperty()); and (object.setProperty(parameters));

my other choice is using get and set, to simplify so I can use

object.property and object.property =
(when accessing or setting a property of an object)

Is this correct? You haven’t explained why the code works just fine with the get and set keywords removed. Is that javascript being nice? like with missing semicolons?


#6

The code doesn’t work unless you change the names of the methods. get and set methods have bindings to the backing variable of the same name. It’s the mechanics under the hood that maintain those binding.

get prop() {
    return this._prop;
}
set prop(value) {
    this._prop = value;
}

It’s the union of the two working in concert that permits this syntax in making a new assignment…

object.prop = newValue;

Using a standard method, we would just execute it…

object.setProp(newValue);

Here it would optional to use a backing variable.

setProp(value) {
    this._prop = value;
}

Underscore props are not private, but they are distinctly different from their mate, so tell the reader (other programmer) that you wish them to be somewhat private.