class Customer {
constructor(name) {
this.name = name;
}
send(amount, to) {
new GooglePay().send(amount, this, to);
}
receive(amount, from) {
console.log(`Payment of ${amount} from ${from} to ${this.name} is succesful`);
}
}
问题基于调解人模式。因此,在上面我已经定义了可以收发资金的客户。因此,我建立了一个名为GooglePay的类,该类可介导客户之间的交易。客户具有通过其可以汇款的发送功能,它需要2个参数(金额为to)
此函数实际上应调用GooglePay实例或由其接收,然后在检查接收者是否已注册后,将金额发送给接收者
class GooglePay {
constructor() {
this.customerBase = [];
}
register(name) {
this.customerBase.push(name);
return this;
}
send(amount, from, to) {
if (this.customerBase.filter(cust => cust === to)) {
to.receive(amount, from);
} else {
console.log('This customer does not exist');
}
}
}
请帮助我,我被困住了,我不明白如何从一个类访问其他类的方法。
是否使用任何框架都没有关系,如果您想按传统方式调用js
类。要从类中调用函数或属性,可以像这样:
//expecting that the class is in another file but same directory
const Gpay = require('./googlePay'); //<-- don't need the .js file extension
const googlePay = new GPay();
现在您可以使用这样的类:
googlePay.register('Name');