Codecademy MealMaker Project after Objects section

Hi all,

I just completed the MealMaker Project, which is at the end of the Objects section. You can view my code here:

or a quicker link for the full view:

ObjectPracticeWithMealMakerProject/MealMaker.js at main · RenSylvain/ObjectPracticeWithMealMakerProject · GitHub

Everything ended up working, but, I only have one question:

On line 13 you’ll see my get courses() method. When I test this method just by its self as you can see I did on line 66, it returns the object with all it’s properties but it also has it’s getters and setters along with it.
like this:
appetizers: [Getter/Setter], mains: [Getter/Setter], desserts: [Getter/Setter]

Is it just always like that when you return an object that has getters and setters? How do I return an object with just its properties?

I am actually kind of surprised the whole project works when the get courses() method returns an object with 3 methods that arent properties.

Anyway, Whatever comments you can provide are great. It was a fund project.

Try the following. Note, this uses the getters…

 get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts

Also, to display currency,

The total of this meal will be ${totalPrice.toFixed(2)}

Your setters are not even used so they can be removed, or make an attempt to use them.

I submitted a pull request to your project with the suggested edits to bring it more in line with what the project intended in the instructions.

Main problem is you created the getters and setters of _course, inside it. The getters should be outside to maintain _course private. The other methods in the menu class should use these setters and getters as would external consumers of Menu, like your tests. Note how your test of step 4 and 8 are calling the private _courses property directly. This is what we are trying to avoid with the getters and setters and the _ naming convention.

Then, as mtf noted, the courses getter should return a new object with the key/value pairs. Ideally here you use the getters and setters outside _courses.

This answers why on your test for step 6 (line 66) you are getting back setters and getters. The project still works as your calls are accessing the _courses directly which has the same property names available, but is going around the encapsulation goal the project is intending to show.

The _property is just a naming convention for objects, as you know. But when you start working with Classes and ES10 you will see you can now set properties as truly private. Your original code would not work in this manner. You will not be able to learn/use private fields in CodeCademy yet as the learning environment is using an older version of node.js which doesn’t support it. But is good to be aware of this.

Hope this helps. Love that you are using github to share and work on your code.

1 Like

Big thanks to both of you. Ivan, I definitely appreciate the feedback on how my code didn’t use the _naming convention properly. That misunderstanding on my part could of turned into a mess in future lessons/coding.

You’ll see on the github repository I’ve approved your changes (first time dealing with a branch? fork? commit?) anyway, lets go with commit. I also did one more commit of my own. I removed all the unnecessary if statements in the addDishToCourse() function, the specific line is #23. As you can see I used the notation, instead of 3 if statements and . (dot) operator, to have the function respond properly to the different values that courseName can have.

Honestly, I don’t completely understand the _propery naming convention 100% yet, but thanks to this thread and the code repository I am sure it’ll sink in over time. I’ll continue to refer back to this place in the future. And I guess maybe take the Codecademy object sessions over again.

But ya, this was interesting since I can see how github is a useful tool to learn from other programmers.

1 Like

Ok, just another two cents. Under the codecademy javascript section: “Learn Javascript Syntax: Objects - Advanced Objects”…I think that’s the only place where they teach the _naming convention. And in that section they don’t use the _naming convention with a nessted object like _courses was in this MealMaker project. They only use the _naming convention with other properties. But I guess an object is a property isn’t it? so I guess I am just gonna have to treat it the way that you guys explain here… Anyway, no excuses haha. :slight_smile: I guess this is how you learn. My code had tons of things to improve on.

Thanks for the input, it was definitely needed.

@megaslick23 Great. If you see I submitted further improvements via Github. There are endless ways to keep improving and refactoring existing software.
The naming convention is just to let other developers know they shouldn’t be accessing or modifying those variables directly. The setters and getters in the examples are very basic and just return the private value or save the new one as received. But in many cases there would be data validation and other steps before the new value received is saved.