I'm trying to get this piece of code to work, but can't figure out why myInvoice.amount
is evaluating to NaN
.
I initially thought it had something to do with the value of this
, but if that were the case, why is myInvoice.total() working correctly?
function createInvoice(services = {}) {
return {
phone: services['phone'] || 3000,
internet: services['internet'] || 5500,
amount: (this.phone + this.internet),
total() {
return this.phone + this.internet;
},
};
}
let myInvoice = createInvoice();
console.log(myInvoice.phone); // 3000
console.log(myInvoice.internet); // 5500
console.log(myInvoice.amount); // NaN
console.log(myInvoice.total()); // //8500
I've also checked the typeof phone
and internet
and they're both numbers. I've also included what gets logged to the console in a comment next to the expression.
Any help would be appreciated!
Thanks