你肯定非常熟悉nodejs模塊中的exports對象,你可以用它創建你的模塊。例如:(假設這是rocker.js文件)
exports.name = function() {
console.log('My name is Lemmy Kilmister');
};
在另一個文件中你這樣引用
var rocker = require('./rocker.js');
rocker.name(); // 'My name is Lemmy Kilmister'
那到底Module.exports是什么呢?它是否合法呢?
其實,Module.exports才是真正的接口,exports只不過是它的一個輔助工具。 最終返回給調用的是Module.exports而不是exports。
所有的exports收集到的屬性和方法,都賦值給了Module.exports。當然,這有個前提,就是Module.exports本身不具備任何屬性和方法。如果,Module.exports已經具備一些屬性和方法,那么exports收集來的信息將被忽略。
修改rocker.js如下:
module.exports = 'ROCK IT!';
exports.name = function() {
console.log('My name is Lemmy Kilmister');
};
再次引用執行rocker.js
var rocker = require('./rocker.js');
rocker.name(); // TypeError: Object ROCK IT! has no method 'name'
發現報錯:對象“ROCK IT!”沒有name方法
rocker模塊忽略了exports收集的name方法,返回了一個字符串“ROCK IT!”。由此可知,你的模塊並不一定非得返回“實例化對象”。你的模塊可以是任何合法的javascript對象--boolean, number, date, JSON, string, function, array等等。
你的模塊可以是任何你設置給它的東西。如果你沒有顯式的給Module.exports設置任何屬性和方法,那么你的模塊就是exports設置給Module.exports的屬性。
下面例子中,你的模塊是一個類:
module.exports = function(name, age) {
this.name = name;
this.age = age;
this.about = function() {
console.log(this.name +' is '+ this.age +' years old');
};
};
可以這樣應用它:
var Rocker = require('./rocker.js');
var r = new Rocker('Ozzy', 62);
r.about(); // Ozzy is 62 years old
下面例子中,你的模塊是一個數組:
module.exports = ['Lemmy Kilmister', 'Ozzy Osbourne', 'Ronnie James Dio', 'Steven Tyler', 'Mick Jagger'];
可以這樣應用它:
var rocker = require('./rocker.js');
console.log('Rockin in heaven: ' + rocker[2]); //Rockin in heaven: Ronnie James Dio
現在你明白了,如果你想你的模塊是一個特定的類型就用Module.exports。如果你想的模塊是一個典型的“實例化對象”就用exports。
給Module.exports添加屬性類似於給exports添加屬性。例如:
module.exports.name = function() {
console.log('My name is Lemmy Kilmister');
};
同樣,exports是這樣的
exports.name = function() {
console.log('My name is Lemmy Kilmister');
};
請注意,這兩種結果並不想同。前面已經提到module.exports是真正的接口,exports只不過是它的輔助工具。推薦使用exports導出,除非你打算從原來的“實例化對象”改變成一個類型
API文檔是枯燥的,下面本人收集了一些論壇經常有人疑問和開源代碼中經常遇到的案例供大家研究一下。
文章博客地址 排版更佳。。
##module.exports與exports的區別
每一個node.js執行文件,都自動創建一個module對象,同時,module對象會創建一個叫exports的屬性,初始化的值是 {}
module.exports = {};
Node.js為了方便地導出功能函數,node.js會自動地實現以下這個語句
foo.js
exports.a = function(){ console.log('a') } exports.a = 1
test.js
var x = require('./foo'); console.log(x.a)
看到這里,相信大家都看到答案了,exports是引用 module.exports的值。module.exports 被改變的時候,exports不會被改變,而模塊導出的時候,真正導出的執行是module.exports,而不是exports
再看看下面例子
foo.js
exports.a = function(){ console.log('a') } module.exports = {a: 2} exports.a = 1
test.js
var x = require('./foo'); console.log(x.a)
result:
2
exports在module.exports 被改變后,失效。
是不是開始有點廓然開朗,下面將會列出開源模塊中,經常看到的幾個使用方式。
##module.exports = View
function View(name, options) { options = options || {}; this.name = name; this.root = options.root; var engines = options.engines; this.defaultEngine = options.defaultEngine; var ext = this.ext = extname(name); if (!ext && !this.defaultEngine) throw new Error('No default engine was specified and no extension was provided.'); if (!ext) name += (ext = this.ext = ('.' != this.defaultEngine[0] ? '.' : '') + this.defaultEngine); this.engine = engines[ext] || (engines[ext] = require(ext.slice(1)).__express); this.path = this.lookup(name); } module.exports = View;
javascript里面有一句話,函數即對象,View 是對象,module.export =View, 即相當於導出整個view對象。外面模塊調用它的時候,能夠調用View的所有方法。不過需要注意,只有是View的靜態方法的時候,才能夠被調用,prototype創建的方法,則屬於View的私有方法。
foo.js
function View(){ } View.prototype.test = function(){ console.log('test') } View.test1 = function(){ console.log('test1') }
module.exports = View
test.js
var x = require('./foo'); console.log(x) //{ [Function: View] test1: [Function] } console.log(x.test) //undefined console.log(x.test1) //[Function] x.test1() //test1
##var app = exports = module.exports = {};
其實,當我們了解到原理后,不難明白這樣的寫法有點冗余,其實是為了保證,模塊的初始化環境是干凈的。同時也方便我們,即使改變了 module.exports 指向的對象后,依然能沿用 exports的特性
exports = module.exports = createApplication; /** * Expose mime. */ exports.mime = connect.mime;
例子,當中module.exports = createApplication改變了module.exports了,讓exports失效,通過exports = module.exports的方法,讓其恢復原來的特點。
##exports.init= function(){}
這種最簡單,直接就是導出模塊 init的方法。
##var mongoose = module.exports = exports = new Mongoose;
集多功能一身,不過根據上文所描述的,大家應該不能得出答案。

