Getter/Setter - Setter Call - Meal Maker

Good day,

In the below project (Meal Maker) i am struggling to fully understand the implementation of the Setter Methods and when they are being called.

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

My Code:

So from my understanding (using set appitizer(appitizerIn) as reference)

line 53:
calls addDishToCourse(courseName)
courseName comes from the input in 53.

line 32:
creates a dish object with the inputs from like 53;
this._courses[courseName] calls set appitizers(appitizersIn)
appitizer in gets the push.(dish)?

the [courseName].push is calling the appropriate set method using bracket notation by using the courseName from 53 and pushing it to the appitizers property array in _courses?

is this correct?

if you insert a .log() into the setter:

  set appitizers(appitizerIn){
      console.log('setter works');
      this._courses.appitizers = appitizerIn;
  },

we don’t see anything. So the setter isn’t used.

The setter is part of menu object, not the courses object. Furthermore, the setter is used when doing assignment. Combining these two pieces of knowledge, we can do:

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
        name: dishName,
        price: dishPrice
      };
    this[courseName] = dish;
  },

then within your setter, you would need to do the push. So now we changed the default assignment behavior of the property

Ah! Okay, i understand now.

I think the reason i got confused was that i had referenced the solution video in which he had created the setters as they are found in my code, and because of this thought they were actually being used, but couldn’t figure out how.

Much appreciated for the help!

Always verify, other developers can mistakes too. You can use a .log() like I did, or a break-point to see if what you expect is actually happening. Or to clarify if things are unclear. Learning to inspect the state of your program and the behavior of your code is very important

actually, if the setter:

  set appitizers(appitizerIn){
      this._courses.appitizers = appitizerIn;
  },

had worked, that would be horrible. You now overwrite/re-assign the property with a string, while it should be an array

Gotcha!

Thanks again! Much appreciated!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.