The javascript syntax part 2/Meal Maker project

  • Once you understand a new concept, come back and try to help someone else!

    in the task 4 of the project i don’t understand this : Create getter and setter methods for the appetizers , mains , and desserts properties.
    i have confused of the solution in the hint below the task : why he make cully braces before the getter setter methods like this :slightly_smiling_face :
    const menu = {
    _courses: {
    appetizers: ,
    get appetizers() {

    set appetizers(appetizerIn) {


    and thanks in advance

no we can access the appetizers directly from menu object through the getter:


even though the property (menu._courses.appetizers) is nested another object deep.

now we can also make a courses getter, which can more easily utilize the appetizers (and others) getter. Not sure I should type this code here now, that might be a spoiler.

my friend y question is why in the hint of task 4 of the meal maker’s project why you have create the (getter/setter) of the (appetizers, mains, desserts] outside of the _courses OBJECT ?

And I cover your question in my answer :wink:

The get syntax binds an object property to a function that will be called when that property is looked up.

so now we have an appetizers property directly in menu, which simplifies accessing a deeper nested property later.

the “problem” with this exercise, is that there are multiple ways to implement getters and setters here (or not use getter and setters altogether).

You can implement the getters within courses object, but then the getters add even less value.

my friend it have problem if i create the (getter/setter) method like this:

const menu = {
_courses : {
get appetizers () {},
set appetizers (app) {},
get mains () {},
set mains (main) {}

That has been covered in the course until this point, right?

trying to access the appetizers property inside the getter, will lookup the property created by the getter, which had a function bound to it, which will lead to endless recursion

your approach would require the property to have a different name, a underscore prefix is a common approach/convention.

Thank you my friend for your help

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