Javascript Meal Maker Project - What do the getters and setters actually do?

I’m working on the Meal Maker Project as a part of the Javascript II course. I’ve worked through it several times to get the understanding of what each line of code is really doing and its purpose (I’m not a quick learner!). As I look at the code it does not seem like the getter and setter methods we create for the _courses properties (appetizers, mains, desserts) come into play at all in the final solution. Am I wrong about this. I took my finished code and deleted those methods and ran it again and there was no error and no change to the output. Is it simply good practice to put those methods in there? Maybe my question is, what was the purpose of instructing us to put the getter and setters in the solution if they didn’t actually do anything?

1 Like

Hello @pycoder84798, welcome to the forums! From what I’ve read/seen, you’re completely right. The getters and setters don’t play a role in the final code. I think you just create them to practise creating them.

I recall in one of the lessons it said that getters & setters shouldn’t have the same name as properties. Doing this project made me realize why— the instructions say to name our setters the same as the properties. Because of this, my getters & setters weren’t being used at all… but when I changed the appetizers, mains, & desserts properties to private properties (e.g. _appetizers, etc), they worked. Now when I used this.appetizers = dish , it referenced the setter method rather than directly mutating the appetizers array.

Hope this all makes sense & helps!