admin管理员组文章数量:1355103
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:
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.
module.exports = (params) => {
initClass(params),
getInstance
}
This plaints that there is no function 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.
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:
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.
module.exports = (params) => {
initClass(params),
getInstance
}
This plaints that there is no function 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.
Share Improve this question edited Jul 19, 2019 at 8:48 madu asked Jul 19, 2019 at 8:21 madumadu 5,47014 gold badges65 silver badges104 bronze badges 5-
1
Regardless of
import/export
, to callinitClass
chained toinit
,init
would have to return something that hasinitClass
on its prototype - which it clearly doesn't – baao Commented Jul 19, 2019 at 8:25 -
1
Your
var init = require('./class.js')(params).initClass(params);
call does not look valid. Or are you returning something from the exported function? – Bergi Commented Jul 19, 2019 at 8:32 -
@Bergi Yes, I am exporting an instantiated class from
initClass
– madu Commented Jul 19, 2019 at 8:36 - 1 @madu Then please show the actual code. – Bergi Commented Jul 19, 2019 at 8:44
-
@Bergi added the code of
initClass
. Thank you. – madu Commented Jul 19, 2019 at 8:48
3 Answers
Reset to default 3You're making it more plex 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. You don't have to pass arguments to the functions at the time you require
.
module.exports is basically an object with keys which can refer to any variables/functions of your file.In your case,
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()
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);
本文标签: javascriptExporting multiple functions with argumentsStack Overflow
版权声明:本文标题:javascript - Exporting multiple functions with arguments - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744036407a2579850.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论