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.