Meal Maker project question


#1

I have a question regarding Meal Make project. According to the exercise, I had to do the following:
Create an object menu
Create a private object _courses
under “_courses” I had to create three blank arrays “appetizers”, “mains” and “desserts” with no privacy settings and consequently I had to create get/set for each array.
under “menu” object I had to create a get call courses() that returns the values of the arrays in courses.

Now here is the problem:
After doing the whole instructions, the exercise threw a return errors on array references. I compared codes from an existing solution and I noticed that all items in _course object is private. So, the problems got fixed after I made them private too.

Exercise instructions:

_courses {
appetizers: [],
mains: [],
desserts:[],
}

Throws return errors and calling the function addDishToCourse threw an error “addDishToCourse is not a function”

Solution found in various places

_courses {
_appetizers: [],
_mains: [],
_desserts:[],
}

Everything is fine

Can someone tell me why I had to make the items private while the instruction didn’t say so?

Thank you


#2

In general, properties with getters and setters are private so that the variable is available for other use.

foobar = {
  _foo: [],
  _bar: [],
  get foo () {
    return this._foo;
  },
  get bar () {
    return this._bar;
  },
  set foo (newFoo) {
    this._foo.push(newFoo);
  },
  set bar (newBar) {
    this._bar.push(newBar);
  }
}

console.log(foobar.foo, foobar.bar);    // [] []
foobar.foo = 'foo';
foobar.bar = 'bar';
console.log(foobar.foo, foobar.bar);    // ['foo'] ['bar']

Notice that all our calls use the public variable?