I am creating a project that defines the following class:
class BankAccount {
constructor(balance=0){
this.balance = balance;
}
withdraw(amount){
if(this.balance - amount >= BankAccount.overdraftlimit){
this.balance -= amount;
}
}
}
BankAccount.overdraftlimit = -500;
My question here is about the definition of the property overdraftlimit
Is this the best way to define let's call a global property? Or should be better to define it inside the constructor
like
this.overdraftlimit = -500;
Thank you!!
If the overdraftlimit
is shared across all the BankAccount
, then go with the static propriety.
BankAccount.overdraftlimit = -500;
If every BankAccount
have their own overdraftlimit
, it should not be part of the class, but part of the single instances.
constructor(balance = 0){
this.balance = balance;
this.overdraftlimit = -500;
}
this way you could also change overdraftlimit
without affecting other BankAccount
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.