导出节点JS中的类

我写了三个函数如下。 我已经在一个包中放入了amp.js和dish.js。

amp.js:

var amp = (function() { //return ("This is a message from the demo package"); function amp(){ this.id=10; } amp.prototype.display=function(){ return 'value of id is'; } }()); exports.amp =amp; 

dish.js:

 var cc=require('./amp'); exports.amp=cc.amp; 

EX.js:

 var builder = require('botbuilder'); var just=require('JUST'); console.log(just); var amp= new just.amp(); console.log(amp); //var ww=new builder.ConsoleConnector(); // Create bot and bind to console var connector = new builder.ConsoleConnector().listen(); var bot = new builder.UniversalBot(connector); bot.dialog('/', function(session){ session.send("hello this is chat bot"); //session.send("string returned from the %s",just.amp.display); }); 

在EX.js中,我试图访问amp.js作为类的构造函数,我得到以下错误,

 E:\bot-prac\EX.js:4 var amp= new just.amp(); ^ TypeError: just.amp is not a function at Object.<anonymous> (E:\bot-prac\EX.js:4:10) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) at node.js:974:3 

下面是一个如何将实用程序/帮助函数存储在外部文件中的示例,然后在您的主app.js代码中加载和使用它们。

 // contents of utils.js module.exports = { init: function() { // do initialization things here }, makeSandwich: function() { // make sandwich code here }, orderPizza: function(options) { // order pizza with options code here } } // END utils.js // contents of app.js var utils = require('./utils'); utils.init(); // call the init function utils.makeSandwich() // call the make sandwich function var pizzaOptions = { toppings: ['pepperoni', 'mushrooms', 'onions', 'olives'], cheese: 'Mozzerella', crust: 'Italian' } utils.orderPizza(pizzaOptions); // END app.js 

有关更多信息,请阅读module.exports核心文档。