我正在嘗試在 javascript 中處理一個簡單的個人帳戶物件。我在物件鍵中創建了一些函式,但是當我試圖控制我的 accountBalance 函式時,它沒有顯示期望輸出。
我正在 accountSummary 鍵中嘗試收入和支出資料,它應該是我 (totalIncome - totalExpense) = 期望輸出,但它在我的終端中給出了 NaN。
`
const personAccount = {
firstName: 'Prashant',
lastName: 'Singh',
incomes: [20000, 30000, 40000],
expenses: [],
totalIncome: function() {
return this.incomes.reduce((acc, curr) => acc curr,0);
},
totalExpense: function() {
return this.expenses.reduce((acc, curr) => acc curr,0);
},
accountInfo: function () {
return `First Name: ${this.firstName}, Last Name: ${this.lastName}, Total Income: ${this.totalIncome()}, Total Expense: ${this.totalExpense()}, Account Balance: ${this.totalIncome() - this.totalExpense()}`
},
addIncome: function (income) {
this.incomes.push(income);
return this.incomes;
},
addExpense: function (expenses){
this.expenses.push(expenses);
return this.expenses;
},
accountBalance: function () {
return this.totalIncome() - this.totalExpense();
},
accountSummary: function () {
return `First Name: ${this.firstName}, Last Name: ${this.lastName}, Balance: ${this.accountBalance()}`
}
}
`
uj5u.com熱心網友回復:
您的代碼應該可以正常作業,這完全取決于您如何訪問它。這是作業演示,請查看并嘗試找到根本原因。
const personAccount = {
incomes: [20000, 30000, 40000],
expenses: [],
totalIncome: function() {
return this.incomes.reduce((acc, curr) => acc curr,0);
},
totalExpense: function() {
return this.expenses.reduce((acc, curr) => acc curr,0);
},
accountBalance: function () {
return this.totalIncome() - this.totalExpense();
}
};
console.log(personAccount.accountBalance());
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520847.html
標籤:javascript数组打字稿目的javascript 对象