原创转载请注明出处:http://agilestyle.iteye.com/blog/2354063
Project Directory
exports.xxx = function() {}
hello.js
exports.world = function() { console.log("Hello World"); };
module.exports = xxx
world.js
function Person(name) { this.name = name; } Person.prototype = { constructor: Person, sayName: function() { console.log(this.name); }, toString: function() { return "[Person " + this.name + "]"; } }; module.exports = Person;
index.js
// exports.world var hello = require('./hello'); hello.world(); // module.exports var world = require('./world'); person = new world(); person.sayName(); person = new world('nodejs'); person.sayName();
Run
Conclusion
外部引用exports.xxx = function() {}和module.exports = xxx模块接口的唯一区别就是后者需要new一下。
Reference
更加详细的解释可以参考