2013-05-19 21 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