require 用来加载代码,而 exports 和 module.exports 则用来导出代码。但很多新手可能会迷惑于 exports 和 module.exports 的区别,为了更好的理解 exports 和 module.exports 的关系,我们先来巩固下 js 的基础。示例:
test.js
var a = {name: 1}; var b = a;
console.log(a); console.log(b);
b.name = 2; console.log(a); console.log(b);
var b = {name: 3}; console.log(a); console.log(b);
运行 test.js 结果为:
{ name: 1 } { name: 1 } { name: 2 } { name: 2 } { name: 2 } { name: 3 }
解释:a 是一个对象,b 是对 a 的引用,即 a 和 b 指向同一块内存,所以前两个输出一样。当对 b 作修改时,即 a 和 b 指向同一块内存地址的内容发生了改变,所以 a 也会体现出来,所以第三四个输出一样。当 b 被覆盖时,b 指向了一块新的内存,a 还是指向原来的内存,所以最后两个输出不一样。
明白了上述例子后,我们只需知道三点就知道 exports 和 module.exports 的区别了:
- module.exports 初始值为一个空对象 {}
- exports 是指向的 module.exports 的引用
- require() 返回的是 module.exports 而不是 exports
现在我们来看 Node.js 官方文档的截图:
我们经常看到这样的写法:
exports = module.exports = somethings
上面的代码等价于:
module.exports = somethings
exports = module.exports
原理很简单,即 module.exports 指向新的对象时,exports 断开了与 module.exports 的引用,那么通过 exports = module.exports 让 exports 重新指向 module.exports 即可。
*** *** 编辑于 2018年03月12日 14:06:51 *** ***
" target="_blank">http://cnodejs.org/topic/52308842101e574521c16e06API文档是枯燥的,下面本人收集了一些论坛经常有人疑问和开源代码中经常遇到的案例供大家研究一下。
IdIdmodule.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 被改变后,失效。
是不是开始有点廓然开朗,下面将会列出开源模块中,经常看到的几个使用方式。
IdIdmodule.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
IdIdvar 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的方法,让其恢复原来的特点。
IdIdexports.init= function(){}
这种最简单,直接就是导出模块 init的方法。
IdIdvar mongoose = module.exports = exports = new Mongoose;
集多功能一身,不过根据上文所描述的,大家应该不能得出答案。