This is driving me nuts as as an ex-ruby developer

menu = {
  _courses: {
  	_appetizers: [],
    _mains: [],
    _desserts: []
  },
  get appetizers() {
    return this._appetizers;
  },
  set appetizers(object) {
    this._courses.appetizers.push(object);
  },
  get mains() {
    return this._mains
  },
  set mains(object) {
    this._courses.mains.push(object)
  },
  get desserts() {
    return this._desserts
  },
  set desserts(object) {
    this._courses.desserts.push(object)
  },
  get courses() {
    return { appetizers: this._appetizers, mains: this._mains, desserts: this._desserts,
    }
  }
}

console.log(menu.courses)

This is returning { appetizers: undefined, mains: undefined, desserts: undefined }. Why are the three empty arrays ‘undefined’ when they are defined in the object at the beginning?.

The problem is in my setter, I want to use the method push on an array but this is impossible because there are ‘undefined’. Thus, I receive the error TypeError: Cannot read property 'push' of undefined.

Missing parent object.

this._courses.appetizers is undefined.

then it should be:

this._courses._appetizers

which is the property, but then you are not using any getters.

the exercise does very cheeky by doing:

menu = {
  _courses: {
  	appetizers: [],
   },
   get appetizers(){ return this._courses.appetizers }

then you can use the getter

Sorry I had to edit my post to make it more clear.

I made an adjustment to make my question more clear.

the problem is still the same one as we highlighted, in the setter:

set appetizers(object) {
    this._courses.appetizers.push(object);
  },

_courses doesn’t have an appetizers property, only a _appetizers property

so you will need to change the setter and or getter.

mtf approach is also possible, within the setters use the appetizers, mains and desserts getters.

appetizers, mains and desserts are properties of courses, not menu.

1 Like

Thank you to both of you! It’s been a long night so I think it is time for me to stop now :wink:

1 Like

Thanks I can see the small mistake now.

1 Like