首页 > 解决方案 > Node.js 如何获取类方法参数并使用它来调用“this.argument”

问题描述

嘿伙计们,我有一个问题。所以我在nodejs中有一个类,在构造函数中我初始化了一些来自uniswap的智能合约。但是,我正在制作一个函数,该函数将我在构造函数中初始化的智能合约作为参数。但是,当我尝试调用该方法并将 uniswapFactoryContract 作为名为“echangeName”的 arg 传递时,节点按原样采用 this.exchangeName,但我没有将 exchangeName 作为 arg 我传入它,而是将其作为 this.echangeName 而不是 this .TheNameOfTheArgIPassIn。我的代码在下面,任何人都可以帮助我或提供另一种方式

module.exports =class Registry {

constructor() {

    //default token addresses
    this.DAI = "0x6b175474e89094c44da98b954eedeac495271d0f";
    this.WETH = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";

    //default provided exhange addressses
    this.UniswapFactoryAddress = "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f";
    this.UniswapRouterAddress = "0xf164fC0Ec4E93095b804a4795bBe1e041497b92a";

    

}

//when i call this in index.js i specifcy the uniswapFactoryaddress as the exchange name paramrer
async getTokenPairSddress(addressToken0, addressToken1,  exchangeName) {

   
    //this line throws the error it thinks i want this.echange name instead of
    //this.uniswapFactoryContract
    const pairAddress = await this.exchangeName.methods.getPair(addressToken0, addressToken1).call();

    return pairAddress;

}

}

标签: javascriptnode.jsclassoop

解决方案


推荐阅读