我想要实现的是创建一个包含多个函数的模块.
module.js:
module.exports = function(firstParam) { console.log("You did it"); },
module.exports = function(secondParam) { console.log("Yes you did it"); },
// This may contain more functions
Run Code Online (Sandbox Code Playgroud)
main.js:
var foo = require('module.js')(firstParam);
var bar = require('module.js')(secondParam);
Run Code Online (Sandbox Code Playgroud)
我firstParam遇到的问题是,它是一个对象类型,secondParam是一个URL字符串,但是当我有它时,它总是抱怨类型错误.
在这种情况下,如何声明多个module.exports?
mas*_*ash 462
你可以这样做:
module.exports = {
method: function() {},
otherMethod: function() {}
}
Run Code Online (Sandbox Code Playgroud)
甚至只是:
exports.method = function() {};
exports.otherMethod = function() {};
Run Code Online (Sandbox Code Playgroud)
然后在调用程序中:
var MyMethods = require('./myModule.js');
var method = MyMethods.method;
var otherMethod = MyMethods.otherMethod;
Run Code Online (Sandbox Code Playgroud)
Dev*_*rah 105
要导出多个函数,您只需将它们列出如下:
module.exports = {
function1,
function2,
function3
}
Run Code Online (Sandbox Code Playgroud)
然后在另一个文件中访问它们:
var myFunctions = require("./lib/file.js")
Run Code Online (Sandbox Code Playgroud)
然后你可以通过调用来调用每个函数:
myFunctions.function1
myFunctions.function2
myFunctions.function3
Run Code Online (Sandbox Code Playgroud)
Far*_*uti 32
除@mash答案外,我建议您始终执行以下操作:
const method = () => {
// your method logic
}
const otherMethod = () => {
// your method logic
}
module.exports = {
method,
otherMethod,
// anotherMethod
};
Run Code Online (Sandbox Code Playgroud)
请注意:
method来otherMethod,你需要这么多您也可以使用相同的技术进行导入:
const {otherMethod} = require('./myModule.js');
Ali*_*Ali 15
这仅供我参考,因为我想要实现的目标可以通过这个来实现.
在里面 module.js
我们可以做这样的事情
module.exports = function ( firstArg, secondArg ) {
function firstFunction ( ) { ... }
function secondFunction ( ) { ... }
function thirdFunction ( ) { ... }
return { firstFunction: firstFunction, secondFunction: secondFunction,
thirdFunction: thirdFunction };
}
Run Code Online (Sandbox Code Playgroud)
在里面 main.js
var name = require('module')(firstArg, secondArg);
Run Code Online (Sandbox Code Playgroud)
小智 10
您可以像我下面那样使用...对于函数和箭头函数:
问候.js:
function greetFromGreet() {
console.log("hello from greet module...");
}
const greetVar = () => {
console.log("greet var as a arrow fn/...");
};
module.exports = { greetVar, greetFromGreet }; // ---- multiple module export...Run Code Online (Sandbox Code Playgroud)
// ----------------------------------------------------------
应用程序.js:
const greetFromGreets = require("./greet");
greetFromGreets.greetFromGreet();
greetFromGreets.greetVar();
Run Code Online (Sandbox Code Playgroud)
// ----------------------------------------------------------
module.js:
const foo = function(<params>) { ... }
const bar = function(<params>) { ... }
//export modules
module.exports = {
foo,
bar
}
Run Code Online (Sandbox Code Playgroud)
main.js:
// import modules
var { foo, bar } = require('module');
// pass your parameters
var f1 = foo(<params>);
var f2 = bar(<params>);
Run Code Online (Sandbox Code Playgroud)
小智 7
您可以这样做的一种方法是在模块中创建一个新对象,而不是替换它.
例如:
var testone = function () {
console.log('test one');
};
var testTwo = function () {
console.log('test two');
};
module.exports.testOne = testOne;
module.exports.testTwo = testTwo;
Run Code Online (Sandbox Code Playgroud)
和打电话
var test = require('path_to_file').testOne:
testOne();
Run Code Online (Sandbox Code Playgroud)
如果文件是使用ES6导出编写的,则可以编写:
module.exports = {
...require('./foo'),
...require('./bar'),
};
Run Code Online (Sandbox Code Playgroud)
有多种方法可以做到这一点,下面提到一种方法。假设您有这样的 .js 文件。
let add = function (a, b) {
console.log(a + b);
};
let sub = function (a, b) {
console.log(a - b);
};
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码片段导出这些函数,
module.exports.add = add;
module.exports.sub = sub;
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码片段使用导出的函数,
var add = require('./counter').add;
var sub = require('./counter').sub;
add(1,2);
sub(1,2);
Run Code Online (Sandbox Code Playgroud)
我知道这是一个迟到的回复,但希望这会有所帮助!
您可以编写一个在其他函数之间手动委派的函数:
module.exports = function(arg) {
if(arg instanceof String) {
return doStringThing.apply(this, arguments);
}else{
return doObjectThing.apply(this, arguments);
}
};
Run Code Online (Sandbox Code Playgroud)
用这个
(function()
{
var exports = module.exports = {};
exports.yourMethod = function (success)
{
}
exports.yourMethod2 = function (success)
{
}
})();
Run Code Online (Sandbox Code Playgroud)
小智 5
你也可以像这样导出它
const func1 = function (){some code here}
const func2 = function (){some code here}
exports.func1 = func1;
exports.func2 = func2;
Run Code Online (Sandbox Code Playgroud)
或者像这样的匿名函数
const func1 = ()=>{some code here}
const func2 = ()=>{some code here}
exports.func1 = func1;
exports.func2 = func2;
Run Code Online (Sandbox Code Playgroud)