简体   繁体   English

从中介模式javascript中的另一个类访问/调用类方法

[英]Accessing/Calling a class method from another class in mediator pattern javascript

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`);
    }
}

the problem is based on mediator pattern.问题是基于调解人模式。 so above i have defined my customer who can send and receive money.所以上面我已经定义了我可以发送和接收资金的客户。 So I have built a class called GooglePay which mediates transaction between customers.因此,我构建了一个名为 GooglePay 的类,用于调解客户之间的交易。 Customer have send function through which they can send money, it takes 2 arguments (amount ,to)客户有发送功能,通过它可以汇款,它需要 2 个参数(金额,到)

The function should actually then invoke or be received by the GooglePay instance which then sends the amount to the receiver after checking if the receiver has registered该函数应该实际调用或被 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');
        }
    }
}

Kindly help me out, I'm stuck and I dont understand how i can access methods of other classes from a class.请帮助我,我被卡住了,我不明白如何从一个类访问其他类的方法。

So i have found the correct solution for my problem, check it out:所以我为我的问题找到了正确的解决方案,请查看:

class Customer {
    constructor(name) {
        this.name = name;
        this.googlepay = null;
    }
    send_money(amount, to) {
        this.googlepay.transaction(amount, this, to);
    }
    receive_money(amount, from) {
        console.log(`payment of ${amount} from ${from} to ${this.name} is succesful`);
    }
}

class GooglePay {
    constructor() {
        this.customer_base = [];
    }
    register_customer(customer_class) {
        this.customer_base.push(customer_class);
        customer_class.googlepay = this;
    }
    transaction(amount, from, to) {
        this.customer_base.filter(cust => {
            if (cust.name === to) {
                cust.receive_money(amount, from.name);
            }
        });
    }
}

on close observation you can see a property in my customer class constructor called this.googlepay = null仔细观察,您可以在我的客户类构造函数中看到一个名为this.googlepay = null的属性

That's where the trick lies.这就是诀窍所​​在。 Cheerios mate Great solving the puzzle. Cheerios mate 很好地解决了这个难题。

Doesn't matter if you're using any frameworks, if you want to call a js class (since ECMAScript 2015 (6th Edition, ECMA-262) ).如果您要调用js类(自ECMAScript 2015 (6th Edition, ECMA-262) 起),是否使用任何框架都没有关系。 To call functions or properties from a class, you can go like:要从类调用函数或属性,您可以像这样:

//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();

Now you can use the class like this:现在你可以像这样使用这个类:

googlePay.register('Name');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM