我正在嘗試構建我的第一個NodeJS模塊。 這是我在做什麼:NodeJS - 導出多個函數
var text = function(){
this.padRight = function(width, string, padding){
return (width <= string.length) ? string : this.padRight(width, string + padding, padding);
};
this.cleanText = function(text){
if (typeof text !== 'undefined') {
return text.replace(/(\r\n|\n|\r)/gm,"");
}
return null;
};
this.printOut = function(outputObj){
var module = this,
output = "";
outputObj.forEach(function(obj){
switch(obj.type){
case "date" :
var date = obj.contents;
if(typeof date != "undefined") output += date.toString().substring(4, 25) + "\t";
break;
case "string":
var string = obj.contents;
if(typeof string != "undefined"){
string = module.cleanText(string);
if(typeof obj.substring != "undefined" && obj.substring != 0) {
string = string.substring(0, obj.substring);
}
if(typeof obj.padRight != "undefined" && obj.padRight != 0) {
string = module.padRight(15, string, " ");
}
output += string + "\t";
}
break;
}
});
console.log(output);
};
};
module.exports.text = text;
我想有不同類型的幫手,所以我希望能夠調用此模塊是這樣的:
require("helpers");
helpers.text.printOut();
但我得到的錯誤。
如何在同一個模塊中導出不同的功能並分別調用它們?
感謝