【问题标题】:Data transfer between 2 classes in JavascriptJavascript中2个类之间的数据传输
【发布时间】:2021-02-21 15:41:25
【问题描述】:

有 2 个等级(AAAA 和 BBBB) 我想将数据从 BBBB 类中的方法 b1 发送到 AAAA 类中的方法 a1。我怎样才能以最简单的方式做到这一点?

我做了以下但没有用,我做错了什么?

谢谢。

class AAAA{
    constructor()
    {
        console.log('AAAA Is Started');
        this.a1 = this.a1.bind(this);
    }

    a1(number1, number2)
    {
        return number1 + number2;
    }
}


class BBBB{
    constructor()
    {
        console.log('BBBB Is Started');
        this.b1 = this.b1.bind(this);
    }

    b1(number1, number2)
    {
        AAAA.a1(number1, number2);
    }
}





$(document).ready(function () {

    const aa = new AAAA();
    const bb = new BBBB();

    bb.b1(2,2);

})

【问题讨论】:

    标签: javascript jquery class


    【解决方案1】:

    是不是因为方法b1没有访问a1的权限? 如何将 AAAA 的新实例传递给 b1 方法,并使用它来调用其方法 a1?

    class AAAA{
        constructor()
        {
            console.log('AAAA Is Started');
            this.a1 = this.a1.bind(this);
        }
    
        a1(number1, number2)
        {
            return number1 + number2;
        }
    }
    
    
    class BBBB{
        constructor()
        {
            console.log('BBBB Is Started');
            this.b1 = this.b1.bind(this);
        }
    
        b1(number1, number2, aa)
        {
            aa.a1(number1, number2);
        }
    }
    
    
    
    
    
    $(document).ready(function () {
    
        const aa = new AAAA();
        const bb = new BBBB();
    
        bb.b1(2,2, aa);
    
    })

    【讨论】:

    • 我不知道如何与这两个类进行交流。非常感谢您的回答。
    • @Frenchy 我不明白。还有其他解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2014-09-05
    • 2021-12-18
    相关资源
    最近更新 更多