Javascript 构造函数函数式银行余额工具
我正在尝试构建一个构造函数。。。一个简单的银行余额概述 这是我的代码:Javascript 构造函数函数式银行余额工具,javascript,function,ecmascript-6,constructor,Javascript,Function,Ecmascript 6,Constructor,我正在尝试构建一个构造函数。。。一个简单的银行余额概述 这是我的代码: class银行账户{ 构造函数(金额=0){ 这.toal=金额; } 余额(金额){ 退还这个金额 } 提取(金额){ 这个.金额-金额; 退还这个金额 } 押金(金额){ 这是金额+金额; 退还这个金额 } }您没有设置此.amount。你应该这样做: this.amount = this.amount-amount; 及 否则金额总是相同的您没有设置this.amount。你应该这样做: this.amount =
class银行账户{
构造函数(金额=0){
这.toal=金额;
}
余额(金额){
退还这个金额
}
提取(金额){
这个.金额-金额;
退还这个金额
}
押金(金额){
这是金额+金额;
退还这个金额
}
}
您没有设置此.amount
。你应该这样做:
this.amount = this.amount-amount;
及
否则金额总是相同的您没有设置
this.amount
。你应该这样做:
this.amount = this.amount-amount;
及
否则金额总是相同的存在某些错误- 在
var account=new bankAccount(10)
中,类名为bankAccount
在构造函数中,您将金额分配给this.total
,之后您将使用this.amount
,它不是类成员。而是将其分配给此.amount
余额方法不需要参数余额(金额)
-->余额()
在draw
方法中,您将扣除参数传递的金额,但最终结果应存储在this.amount
中<代码>this.amount=this.amount-金额代码>。与存款法类似
class银行账户{
构造函数(金额=0){
这个。金额=金额;
}
余额(){
退还这个金额
}
提取(金额){
this.amount=this.amount-金额;
退还这个金额
}
押金(金额){
this.amount=this.amount+金额;
退还这个金额
}
}
var账户=新银行账户(10)
账户提款(2)
账户提款(5)
存款账户(4)
存款账户(1)
console.log(account.balance())//8
存在某些错误-
在var account=new bankAccount(10)
中,类名为bankAccount
在构造函数中,您将金额分配给this.total
,之后您将使用this.amount
,它不是类成员。而是将其分配给此.amount
余额方法不需要参数余额(金额)
-->余额()
在draw
方法中,您将扣除参数传递的金额,但最终结果应存储在this.amount
中<代码>this.amount=this.amount-金额代码>。与存款法类似
class银行账户{
构造函数(金额=0){
这个。金额=金额;
}
余额(){
退还这个金额
}
提取(金额){
this.amount=this.amount-金额;
退还这个金额
}
押金(金额){
this.amount=this.amount+金额;
退还这个金额
}
}
var账户=新银行账户(10)
账户提款(2)
账户提款(5)
存款账户(4)
存款账户(1)
console.log(account.balance())//8
您需要分配表达式的返回值,如下所示:
(还要注意total
中的打字错误)
class银行账户{
构造函数(金额=0){
这个总数=金额;
}
余额(金额){
退还这个金额
}
提取(金额){
this.amount=this.amount-金额;
退还这个金额
}
押金(金额){
this.amount=this.amount+金额;
退还这个金额
}
}
您需要指定表达式的返回值,如下所示:
(还要注意total
中的打字错误)
class银行账户{
构造函数(金额=0){
这个总数=金额;
}
余额(金额){
退还这个金额
}
提取(金额){
this.amount=this.amount-金额;
退还这个金额
}
押金(金额){
this.amount=this.amount+金额;
退还这个金额
}
}
this.amount-amount代码>等。。。不更改金额的值,您需要通过重新分配它来更新它(this.amount=this.amount-amount;
)以及新的BankAccount,而不是代码中的BankAccountthis.amount-amount代码>等。。。不更改金额的值,您需要通过重新分配它来更新它(this.amount=this.amount amount;
)以及新的BankAccount,而不是代码中的BankAccount