var name; exports.setName = function (thyName) { name = thyName; }; exports.sayHello = function () { console.log('Hello ' + name); };
function Hello() { var name; this.setName = function (thyName) { name = thyName; }; this.sayHello = function () { console.log('Hello ' + name); }; }; exports.Hello = Hello;
上面两块代码,均可返回一个exports对象,两者的区别在于,前者,exports对象中有setName、sayHello方法,后者,exports对象中又有Hello对象,其中Hello对象有setName、sayHello方法。
再看
function Hello() { var name; this.setName = function (thyName) { name = thyName; }; this.sayHello = function () { console.log('Hello ' + name); }; }; module.exports = Hello;
这个模块,返回的也是exports,但它不算是对象,仅是Hello对象的引用。
其中外部若想获得Hello对象,第二块代码需要通过require('第二块代码').Hello,而第三块代码则仅需要require('第三块代码')即可获得Hello对象。
不知道可不可以这么说:exports是个打酱油的。