Meal Maker works without getters and setters. WHY?

After playing around with Meal Maker project I came to a discovery that the code instructed by a CodeCademy developer works without getters, setters and get courses() method. What was the purpose of including them if they are useless???

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  
    /*get appetizers() {
      return this.appetizers;
    },
    get mains() {
      this.mains;
    },
    get desserts() {
      this.desserts;           
    },

    set appetizers(appIn) {
      this.appetizers = appIn;
    },
    set mains(mainsIn) {
      this.mains = mainsIn;
    },      
    set desserts(dessIn) {      
      this.desserts = dessIn;
    },

  get courses() {
    return {
      appetizers: this.appetizers,
      main: this.mains,
      desserts: this.desserts
    }
  },*/

//this method add dishes to the Courses object:
  addDishToCourse(courseName, dishName, dishPrice) {
    let dish = {
      name: dishName,
      price: dishPrice,
    };
    return this._courses[courseName].push(dish)
  },
// this method gets the argument value from generateRandomMeal method when called 
  getRandomDishFromCourse(courseName) {
    let dishes = this._courses[courseName];
    let randomIndex = Math.floor(Math.random() * dishes.length);
    console.log(randomIndex);
    console.log(dishes[randomIndex]);
    return dishes[randomIndex];
    
  },
//this method call getRandomDishFromCourse method and saves the object value in a variable
  generateRandomMeal() {
    let appetizer = this.getRandomDishFromCourse('appetizers');
    let main = this.getRandomDishFromCourse('mains');
    let desserts = this.getRandomDishFromCourse('desserts');
    let totalPrice = appetizer.price + main.price + desserts.price;
    return `The total price for ${appetizer.name}, ${main.name} and       
            ${desserts.name} is ${totalPrice}`;
  }
}

menu.addDishToCourse('mains', 'Pelmeni', 50);
menu.addDishToCourse('mains', 'Manti', 40);
menu.addDishToCourse('mains', 'Plov', 60);
menu.addDishToCourse('desserts', 'Tort', 20);
menu.addDishToCourse('desserts', 'Choco', 21);
menu.addDishToCourse('desserts', 'Ice Cream', 12);
menu.addDishToCourse('appetizers', 'Oreshki', 20);
menu.addDishToCourse('appetizers', 'Blini', 10);
menu.addDishToCourse('appetizers', 'Chips', 15);

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

one of the things we could do is something along the lines of:

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  }
}

now we can do menu.appetizers even though the underlying property (_appetizers) is nested another object deep.

and if we implement the setters to push the elements into the array:

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appIn) {
    this._courses.appetizers.push(appIn);
  },
}

we can shorten certain tasks:

// old
return this._courses[courseName].push(dish)
// using the setter:
return this[courseName] = dish
2 Likes

getters and setters are good for accessing deeper fields… but another very important value of getters and setters are to control what kind of content is accepted. For example: const myObj = {_name: " "}; If we want to allow the setting of the name property we can add a setter method to our myObj object – set name(newName){
this._name = newName;
}
However, this allows for users to set numbers to the name property which may not be a good idea if we expect this name field to be a string. We can make sure that the user doesnt try something like myObj.name(100); by adding extra logic to our setter method: set name(newName){ if(typeof newName !== “string”) { console.log("name method only takes strings as arguments. "); return; } else { this._name = newName;}
The getter function can also be added to. Maybe you want the property returned to first undergo some formatting…

1 Like