【发布时间】:2019-09-14 13:34:08
【问题描述】:
我正在尝试构建一个构造函数...一个简单的银行余额概览。
这是我的代码:
class BankAccount {
constructor(amount = 0) {
this.toal = amount;
}
balance(amount) {
return this.amount
}
withdraw(amount) {
this.amount - amount;
return this.amount
}
deposit(amount) {
this.amount + amount;
return this.amount
}
}
这是我期望的例子。
// 示例
var account = new bankAccount(10) account.withdraw(2) account.withdraw(5) account.deposit(4) account.deposit(1) account.balance() // 8
它不起作用。我哪里错了?
【问题讨论】:
-
this.amount-amount;等...不会更改金额的值,您需要自己通过重新分配来更新它(this.amount = this.amount-amount;) -
也是新的 BankAccount,而不是您代码中的 bankAccount
标签: javascript function ecmascript-6 constructor