Meal Maker

Hi team,

Working on the meal maker project - may I know what the purpose of the setter and getter methods in the coding? It makes no difference when I “comment” it out from the code.

let menu = {
_courses: {
appetizers: ,
mains: ,
/*get appetizers () {
return this._courses.appetizers
set appetizers (appetizers){
this._courses.appetizers = appetizers
get mains () {
return this._courses.mains
set mains (mains){
this._courses.mains = mains
get desserts () {
return this._courses.desserts
set desserts (desserts){
this._courses.desserts = desserts
get courses (){
return {
appetizers: this.appetizers,
mains: this.mains,
desserts: this.desserts
}, */
addDishToCourse (courseName, dishName, dishPrice) {
const dish = {
name : dishName,
price : dishPrice,
return this._courses[courseName].push(dish)
getRandomDishFromCourse (courseName) {
const dishes = this._courses[courseName]
const randomIndex = Math.floor(Math.random()*dishes.length)
return dishes[randomIndex]
getRandomMeal () {
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 ${}, ${}, and ${}. The total price is ${totalPrice}.


In your variant of code you don’t use getters and setters, so you can remove them. But in this exercise you have to use getters and setters.

In real project getters and setters can be used for several reasons:

  1. To hide internal structure of object. By JS convention, fields with names started from underscore _ is for private use inside object. It is bad idea to access those fields outside, because author of code can change internal object structure wihout notification. But author can provide getters and setters, which always correctly access internal structures.
  2. Another reason - modification or validation of data before put into object. For example, in set appetizers (appetizers) {} you can validate are there name and prices fields in provided objects and throw an error if not. In getter you can, for example, recalculate price before return result if discount applied.

I hope the explanation was clear enough because my English is bad)


Thank you, this is the most clear explanation of the reasoning behind this that I’ve found!