Meal Maker - underscore

Hi guys, I am totally new to programming making my way through the fullstack course. I am not exactly stuck but I don’t quite understand why we are not using an underscore for the object key.

During the lesson:

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)

In meal maker the hints shows no underscore so key value is identical to the get method:

const menu = {
_courses: {
appetizers: ,
…,
},
get appetizers() {
},
set appetizers(appetizerIn) {
},

One explanation I found on the forum is that it’s because we are dealing with nested object but as all: appetizers, mains, desserts and getters and setters are all part of the _courses object shouldn’t we use an underscore to differentiate?

I am obviously missing something just cant see what :slight_smile:

Hi @micro4294342361
Welcome to the forums!
The underscore has semantic reasons only. Whether or not you prepend an object key with an underscore has no effect on the functionality. It is just a hint that this property is not supposed to be accessed from the outside. You find an explanation in the lesson ‘Advanced objects’, chapter Privacy:

Certain languages have privacy built-in for objects, but JavaScript does not have this feature. Rather, JavaScript developers follow naming conventions that signal to other developers how to interact with a property. One common convention is to place an underscore _ before the name of a property to mean that the property should not be altered.

The setters and getters don’t use the underscore for two reasons:

  1. To avoid a duplicate key
  2. These methods are meant to provide access from the outside

PS: You can format your code when posting it here by marking it and then clicking the </> sign in the editor.

Many thanks for the explanation! I remembered the privacy bit but somehow I was under the impression that you also need to differentiate the name in getters/setters from the key they are referring to .

And thanks for the code tip!

1 Like

what helped me actual understand this problem is by simulating the problem:

// do NOT run this code
const example = {
   a: 3,
   get a() {
     return this.a;
   }
}

Now doing example.a would result in an error, the getter would endlessly call itself and we can not access the a: 3 property/value.

so we could do something like:

const example = {
   a: 3,
   get b() {
     return this.a;
   }
}

now doing console.log(example.b) would give us: 3

but this is confusing. a = b? what that doesn’t make sense

so then the underscore is an agreed upon convention

we don’t have this problem when the property is in an nested object (like in meal maker), the getter is now a convenient way to access the nested property (menu.appetizers)

no, this notion is not correct. You need to prevent that getter will invoke/lookup itself. And it is wise to follow convention, but if you have a very good reason (which is rarely the case), you can of course deviate. But its important to understand how it all works :slight_smile:

Thanks for help, i think there is a lot to wrap your head around especially in the beginning but what you’re saying makes sense.

And sorry - I did run the first code just to see what happens :open_mouth:

1 Like