简体   繁体   English

使用参数导出多个函数

[英]Exporting multiple functions with arguments

All I am trying to do is to export two functions from a module. 我要做的就是从模块中导出两个函数。 One function taking an argument and the other with no argument: 一个函数带有一个参数,另一个不带参数:

function initClass(params)
{
   return new Promise( (resolve, reject) => {
        if (!wallet) {
            wallet = new WalletClient(params);
            resolve(wallet);
        } else {
            console.log('Wallet is initialized');
            resolve(wallet);
        }
});
}

function getInstance()
{
   return wallet;
}

For initClass(params) only, I can do this as: 仅对于initClass(params) ,我可以这样做:

module.exports = (params) => {
   initClass(params)
}

And then I call this as: 然后我将其称为:

var init = require('./class.js')(params).initClass(params);

This works fine. 这很好。

Now, for me to export getInstance() as well, I have tried to do the following but it doesn't seem to work. 现在,对于我也要导出getInstance() ,我尝试执行以下操作,但似乎不起作用。

module.exports = (params) => {
   initClass(params),
   getInstance
}

This complaints that there is no function getInstance . 这抱怨说没有函数getInstance

Then I tried this: 然后我尝试了这个:

module.exports.init = (params) => {
   initClass(params)
}

module.exports.instance = {
   getInstance
}

Then call them as: 然后称他们为:

var init = require('./class.js').init(params).initClass(params);

What is the proper way to export multiple functions like this? 导出这样的多个函数的正确方法是什么? Thank you. 谢谢。

If i'm understanding correctly you are trying to export multiple methods if that is the case simple use this. 如果我理解正确,那么您尝试导出多种方法(如果是这种情况,请简单地使用此方法)。

   module.exports = {
        method: function() {},
        otherMethod: function(parmas) {}
    }

In your code use like this. 在您的代码中使用这样的代码。

 var init = require('./class.js');
    init.method()
    init.otherMethond(paramObj)

If you want below scenario you need to check out about method chaining. 如果要在以下情况下使用,则需要查看有关方法链接的信息。

var init = require('./class.js').init(params).initClass(params);

You're making it more complex than needed. 您使它变得比所需的更加复杂。 Once you have your functions defined, you can export it with this: 定义函数后,可以使用以下命令将其导出:

module.exports = {
  initClass,
  getInstance
}

To use it, you do this: 要使用它,请执行以下操作:

const init = require("./class.js");
init.initClass(params);
const instance = init.getInstance();

What you're exporting from the module is an object (which I've named init in the example above) that contains two functions. 从模块中导出的是一个对象(在上面的示例中我将其命名为init ),该对象包含两个函数。 You don't have to pass arguments to the functions at the time you require . 你不必在你的时间传递函数的参数require

module.exports is basically an object with keys which can refer to any variables/functions of your file.In your case, module.exports基本上是一个带有键的对象,该键可以引用文件的任何变量/函数。

module.exports.initClass = function (params){
...
}

module.exports.getInstance = function (){
}

When importing it 导入时

var init = require('./class.') // init object has two keys -> initClass and getInstance
 init.initClass('abc')
 init.getInstance()

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

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