I don't understand the 'Meal Maker' code. Why are underscores used?


#1

In the’Meal Maker’ exercise

After a lot of help from this forum and elsewhere I got my code to run after experiencing a lot of frustration - it turns out I had to include underscores for appetizers, mains and desserts. But I don’t fully understand why. Can anyone help me learn by explaining why the underscores are needed.
Here is my code.

const menu={
_courses:{
_appetizers:,
_mains:,
_desserts:,

get appetizers(){
return this._appetizers;
},
set appetizers(appetizersIn){
this.appetizers=appetizersIn;
},
get mains(){
return this._mains;
},
set mains(mainsIn){
this.mains=mainsIn;
},

get desserts(){
return this._desserts;
},
set desserts(dessertsIn){
this.desserts=dessertsIn;
},

},

get courses(){
return {
appetizers:this._courses.appetizers,
mains:this._courses.mains,
desserts:this._courses.desserts
}
},
addDishToCourse (courseName,dishName,dishPrice){
const dish= {
name:dishName,
price:dishPrice,
};
this._courses[courseName].push(dish);
},
getRandomDishFromCourse(courseName){
const dishes=this._courses[courseName];
const randomIndex = Math.floor(Math.random() * dishes.length);
return dishes[randomIndex];
},
generateRandomMeal(){
const appetizer=this.getRandomDishFromCourse(‘appetizers’);
const main=this.getRandomDishFromCourse(‘mains’);
const dessert=this.getRandomDishFromCourse(‘desserts’);
const totalPrice=appetizer.price + main.price + dessert.price;
return Your meal is ${appetizer.name}, ${main.name} and ${dessert.name}. The price is $${totalPrice}.;
}
};

menu.addDishToCourse(‘appetizers’, ‘Ceasar Salad’, 4.25);
menu.addDishToCourse(‘appetizers’, ‘Prawn Coctail’, 4.25);
menu.addDishToCourse(‘appetizers’, ‘Garlic Bread’, 3.50);

menu.addDishToCourse(‘mains’, ‘Lasagna’, 9.75);
menu.addDishToCourse(‘mains’, ‘Ribeye Steak’, 14.95);
menu.addDishToCourse(‘mains’, ‘Fish & Chips’, 12.95);

menu.addDishToCourse(‘desserts’, ‘Cheese Cake’, 4.50);
menu.addDishToCourse(‘desserts’, ‘Creme Brule’, 4.25);
menu.addDishToCourse(‘desserts’, ‘Cheese Board’, 3.25);

let meal=menu.generateRandomMeal();
console.log(meal);


#2

its explained here:

https://stackoverflow.com/questions/47146106/why-does-my-javascript-getter-setter-require-underscores

essentially its to prevent an “infinity loop” (the getter or setter triggering itself)


#3

Thank you so much-that makes sense now.