Why we used getters and setter method in Meal Maker Project?


I am just confused that why are we using getters and setters method in it and I am also confused about a lot of concepts used in this project
I somehow managed to run the code.
Can someone please help me in understanding the concepts used in this project in deep ?

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,
  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', 500.2);
menu.addDishToCourse('appetizers', 'Prawn Coctail', 340);
menu.addDishToCourse('appetizers', 'Garlic Bread', 231);
menu.addDishToCourse('mains', 'Lasagna', 320);
menu.addDishToCourse('mains', 'Ribeye Steak', 1000);
menu.addDishToCourse('mains', 'Fish & Chips', 3040);
menu.addDishToCourse('desserts', 'Cheese Cake', 121);
menu.addDishToCourse('desserts', 'Creme Brule', 130);
menu.addDishToCourse('desserts', 'Cheese Board', 293);
let meal = menu.generateRandomMeal();

one of the things we could do:

const menu = {
  _courses: {
       appetizers: [],
  get appetizers() {
    return this._courses;
  get courses() {
    return {
     appetizers: this.appetizers

now we can access the properties (getter) directly (using menu.appetizers), despite the property being nested inside another object.

furthermore, we could use the setter to push elements into the array:

set appetizers(appetizerIn){

so now we can do:

menu.appetizers = 'something'

which will push the element in the array. We changed the default assignment behavior.

I am still unable to understand properly
I also didn’t understood about getters and setters from codecademy javascript tutorials
Can u clear me the basics of these methods and then explain why they were used in this project ?
I am really sorry if I am asking too much

But if you don’t understand all of this, how did you write the code you included in your post?

getters and setters allow us more control over properties, we can validate values when doing assignment with setter, we could manipulate the property with the getter. The tricky thing with programming is that can learn many of the concepts, but then you start to understand/grasp the true value of such a concept when you build larger projects

what methods? You can always consult external documentation, there is no shame in it. In facet, consulting guides/documentation to clarify things is a good thing.

you can also do this project without getters and setters, they don’t add much value at the moment given they implement default behavior at the moment

Thanks for guiding!
You are a really helpful guy who is helping people around
Thanks once again!

Did I answer all of your questions?

I would advise to do this project without getter and setters, then you can slowly try a getter? And slowly try to if can use a getter somewhere, and see what advantages getters can have

1 Like