2013-05-19 24 views
90

我想要实现的是创建一个包含多个函数的模块。在Node.js中声明多个module.exports

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 

main.js:

var foo = require('module.js')(firstParam); 
var bar = require('module.js')(secondParam); 

我就是firstParam是对象类型的问题和secondParam是一个URL字符串,但是当我有它总是抱怨这种类型是错误的。

如何在这种情况下声明多个module.exports?

+0

我很明显错过了这个范例的一些关键部分,因为它让我失去了这个工作所需要的东西。 –

回答

231

你可以这样做:

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

甚至只是:

exports.method = function() {}; 
exports.otherMethod = function() {}; 
在调用程序

然后:

var MyMethods = require('./myModule.js'); 
var method = MyMethods.method; 
var otherMethod = MyMethods.otherMethod; 
+8

总是使用'module.exports = {}'而不是'module.method = ...'。 http://stackoverflow.com/a/26451885/155740 – Scotty

+3

我没有在这里的任何地方使用'module.method' ......只有'exports.method',它只是对'module.exports.method'的引用,所以表现方式也一样。唯一的区别是我们没有定义'module.exports',所以它默认为'{}',除非我错了。 – mash

+0

@mash可以在另一个文件中使用:var otherMethod = require('module.js')(otherMethod);'?也就是说,该行需要'otherMethod'函数,就像它是页面上唯一的函数,并且导出是:'module.exports = secondMethod;'? – YPCrumble

6

您可以编写一个函数之间手动代表其他功能:

module.exports = function(arg) { 
    if(arg instanceof String) { 
     return doStringThing.apply(this, arguments); 
    }else{ 
     return doObjectThing.apply(this, arguments); 
    } 
}; 
+0

这是一种实现函数重载的方法,但它不是很优雅。我认为Mash的答案更清晰,更好地表明意图。 – Nepoxx

10

这只是为了我的参考,因为我试图实现的目标可以通过这个来实现。

module.js

我们可以做这样的事情

module.exports = function (firstArg, secondArg) { 

    function firstFunction () { ... } 

    function secondFunction () { ... } 

    function thirdFunction () { ... } 

     return { firstFunction: firstFunction, secondFunction: secondFunction, 
thirdFunction: thirdFunction }; 

    } 

main.js

var name = require('module')(firstArg, secondArg); 
0
module.exports = (function() { 
    'use strict'; 

    var foo = function() { 
     return { 
      public_method: function() {} 
     }; 
    }; 

    var bar = function() { 
     return { 
      public_method: function() {} 
     }; 
    }; 

    return { 
     module_a: foo, 
     module_b: bar 
    }; 
}()); 
2

使用本

(function() 
{ 
    var exports = module.exports = {}; 
    exports.yourMethod = function (success) 
    { 

    } 
    exports.yourMethod2 = function (success) 
    { 

    } 


})(); 
3

你可以做的一种方法是在模块中创建一个新对象,而不是替换它。

例如:

var testone = function() 
{ 
    console.log('teste one'); 
}; 
var testTwo = function() 
{ 
    console.log('teste Two'); 
}; 
module.exports.testOne = testOne; 
module.exports.testTwo = testTwo; 

,并呼吁

var test = require('path_to_file').testOne: 
testOne(); 
42

导出多个功能,你可以一一列举如下:

module.exports = { 
    function1, 
    function2, 
    function3 
} 

然后访问它们的另一file:

var myFunctions = require("./lib/file.js") 

然后你就可以通过调用调用各功能:除

myFunctions.function1 
myFunctions.function2 
myFunctions.function3 
+1

真棒...像一个魅力工作 –

+0

完美的答案,这个答案应该标记为正确的答案。 –

12

到@mash答案,我建议您始终做到以下几点:

const method =() => { 
    // your method logic 
} 

const otherMethod =() => { 
    // your method logic 
} 

module.exports = { 
    method, 
    otherMethod, 
    // anotherMethod 
}; 

注意这里:

  • 你可以致电methodotherMethod,你将需要这个很多
  • 您可以快速隐藏的方法为私有,当你需要
  • 这是大多数IDE的理解和自动完成你的代码更容易;)
  • 您还可以使用进口同样的技术:

    const {otherMethod} = require('./myModule.js');

+0

请注意,这使用es6对象初始化器快捷方式 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer – chrismarx