[英]The value returned for a newly constructed object returns the value of the previously created one
I have the following challenge for practicing OOP in javascript:我在 javascript 中练习 OOP 有以下挑战:
The Bank should allow us to:世行应允许我们:
Add customers添加客户
So I did the following, I created a Bank class with different methods.所以我做了以下,我用不同的方法创建了一个 Bank class。
It seems to work fine with 1 person, but my problem is once I add a new customer, and run the methods on the new customer it returns the value of the first customer.它似乎与一个人一起工作正常,但我的问题是,一旦我添加了一个新客户,并在新客户上运行这些方法,它就会返回第一个客户的价值。
class Bank { constructor() { this.customers = []; } addCustomer(customer) { this.customers.push({ name: customer, account: 0 }); } printAccount(customer) { if (this.customers.some((person, idx) => person.name === customer)) { console.log( `${this.customers[0].name}'s account is ${this.customers[0].account}` ); } else { console.log("no access"); } } depositAmount(customer, amount) { if (this.customers.some(person => person.name === customer)) { this.customers[0].account += amount; } else { return; } } withdrawAmount(customer, amount) { if (this.customers.some(person => person.name === customer)) { this.customers[0].account -= amount; } else { return; } } } const bank = new Bank(); bank.addCustomer("daniel"); bank.depositAmount("daniel", 10); bank.withdrawAmount("daniel", 5); bank.printAccount("daniel"); bank.addCustomer("gwen"); bank.depositAmount("gwen", 10); bank.printAccount("gwen"); console.log(bank);
You're always using this.customers[0]
in your methods, so it always operates on the first customer.你总是在你的方法中使用
this.customers[0]
,所以它总是对第一个客户进行操作。
Use find()
to find the customer object, and use that.使用
find()
找到客户 object,并使用它。
class Bank { constructor() { this.customers = []; } addCustomer(customer) { this.customers.push({ name: customer, account: 0 }); } getCustomer(name) { return this.customers.find(person => person.name == name); } printAccount(name) { const customer = this.getCustomer(name); if (customer) { console.log( `${customer.name}'s account is ${customer.account}` ); } else { console.log("no access"); } } depositAmount(name, amount) { const customer = this.getCustomer(name); if (customer) { customer.account += amount; } } withdrawAmount(name, amount) { this.depositAmount(name, -amount); } } const bank = new Bank(); bank.addCustomer("daniel"); bank.depositAmount("daniel", 10); bank.withdrawAmount("daniel", 5); bank.printAccount("daniel"); bank.addCustomer("gwen"); bank.depositAmount("gwen", 10); bank.printAccount("gwen"); console.log(bank);
You should use find
instead:您应该改用
find
:
depositAmount(customer, amount) {
const customer = this.customers.find((person) => person.name === customer);
if (!customer) return; // no customer exists
customer.account += amount; // add to customer
}
If there was no customer found, it returns undefined
and you can check for that.如果没有找到客户,它会返回
undefined
,您可以检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.