The following two codes are equivalent and provide the same results:
1
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
get appetizers() {
return this._courses.appetizers;
},
set appetizers(appetizer) {
this._courses.appetizers= appetizer;
}
}
menu.appetizers.push(['food']);
console.log(menu.appetizers);
2
const menu = {
_courses: {
appetizers: [],
mains: [],
desserts: [],
},
}
menu._courses.appetizers.push(['food']);
console.log(menu._courses.appetizers);
1st method uses getter/setter whereas second directly access the property, so my question is..for me 2nd method is more understandable then why getter/setter is "Better" way?
There are many threads proposing why getter/setter should be used, but I was curious to know for the example I have given, which way is better?