module.exports與exports
每一個(gè)node.js執(zhí)行文件,都自動(dòng)創(chuàng)建一個(gè)module對(duì)象,同時(shí),module對(duì)象會(huì)創(chuàng)建一個(gè)叫exports的屬性,初始化的值是 {}
module.exports = {};
Node.js為了方便地導(dǎo)出功能函數(shù),node.js會(huì)自動(dòng)地實(shí)現(xiàn)以下這個(gè)語句。
foo.js:
exports.a = function(){
console.log('a')
}
test.js:
var x = require('./foo');
console.log(x.a)
看到這里,相信大家都看到答案了,exports是引用 module.exports的值。module.exports 被改變的時(shí)候,exports不會(huì)被改變,而模塊導(dǎo)出的時(shí)候,真正導(dǎo)出的執(zhí)行是module.exports,而不是exports。
Node.js為了方便地導(dǎo)出功能函數(shù),node.js會(huì)自動(dòng)地實(shí)現(xiàn)以下這個(gè)語句
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 被改變的時(shí)候,exports不會(huì)被改變,而模塊導(dǎo)出的時(shí)候,真正導(dǎo)出的執(zhí)行是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)//---> 2
exports在module.exports 被改變后,失效。
是不是開始有點(diǎn)廓然開朗,下面將會(huì)列出開源模塊中,經(jīng)??吹降膸讉€(gè)使用方式。
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里面有一句話,函數(shù)即對(duì)象,View 是對(duì)象,module.export =View, 即相當(dāng)于導(dǎo)出整個(gè)view對(duì)象。外面模塊調(diào)用它的時(shí)候,能夠調(diào)用View的所有方法。不過需要注意,只有是View的靜態(tài)方法的時(shí)候,才能夠被調(diào)用,prototype創(chuàng)建的方法,則屬于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 = {};
其實(shí),當(dāng)我們了解到原理后,不難明白這樣的寫法有點(diǎn)冗余,其實(shí)是為了保證,模塊的初始化環(huán)境是干凈的。同時(shí)也方便我們,即使改變了 module.exports 指向的對(duì)象后,依然能沿用 exports的特性
exports = module.exports = createApplication;
/**
* Expose mime.
*/
exports.mime = connect.mime;
例子,當(dāng)中module.exports = createApplication改變了module.exports了,讓exports失效,通過exports = module.exports的方法,讓其恢復(fù)原來的特點(diǎn)。
exports.init= function(){}
這種最簡(jiǎn)單,直接就是導(dǎo)出模塊 init的方法。
var mongoose = module.exports = exports = new Mongoose;
集多功能一身,不過根據(jù)上文所描述的,大家應(yīng)該不能得出答案。