繁体   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