簡體   English   中英

如何修復我的代碼:錯誤:未捕獲的 ReferenceError:未定義帳戶?

[英]How do I fix my code :error: Uncaught ReferenceError: account is not defined?

我目前正在學習優雅的 JS

我正在編寫代碼,但我無法使用這段代碼。

這是一段簡單的 Javascript 代碼。 老實說,我認為我做對了,但我不斷收到這個錯誤,我對自己做錯了什么感到完全困惑。

 let myAccount = { name: `John Smith`, expenses: 0, income: 0 } let addExpense = function(account, amount) { account.expenses = account.expenses + amount } let addIncome = function(account, income) { account.income = account.income + income } let resetAccount = function() { account.expenses = 0 account.income = 0 } let getAccountSummary = function(account) { let balance = account.income - account.expenses return `Account for ${myAccount.name} has £${balance}. ${myAccount.income} in income £${myAccount.expenses}` } addIncome(myAccount, 2000) addExpense(myAccount, 2.50) addExpense(myAccount, 160) console.log(getAccountSummary(myAccount)) resetAccount(myAccount) console.log(getAccountSummary(myAccount))

我不斷收到Uncaught Reference error.

我已經看了一個多星期了。 完全迷路了

改變

let resetAccount = function() {

  account.expenses = 0
  account.income = 0
}

let resetAccount = function(account) {

  account.expenses = 0
  account.income = 0
}
 let resetAccount = function(){
    
      account.expenses = 0
      account.income = 0
    }

肯定 resetAccount 會導致Uncaught ReferenceError: account is not defined ,您可能希望將 account 參數添加到 resetAccount function。

更新

讓 resetAccount = 函數(帳戶){

  account.expenses = 0
  account.income = 0
}

'resetAccount' function 應該有一個參數。

let resetAccount = function(account) {
  account.expenses = 0
  account.income = 0
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM