我理解的exports 和 module.exports 的区别,欢迎大家吐槽~
为了更好的理解 exports 和 module.exports 的关系,我们先来补点 js 基础。示例:
app.js
var a = {name: 'nswbmw 1'};
var b = a;
console.log(a);
console.log(b);
b.name = 'nswbmw 2';
console.log(a);
console.log(b);
var b = {name: 'nswbmw 3'};
console.log(a);
console.log(b);
运行 app.js 结果为:
D:\>node app
{ name: 'nswbmw 1' }
{ name: 'nswbmw 1' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 2' }
{ name: 'nswbmw 3' }
D:\>
解释一下:a 是一个对象,b 是对 a 的引用,即 a 和 b 指向同一个对象,即 a 和 b 指向同一块内存地址,所以前两个输出一样。当对 b 作修改时,即 a 和 b 指向同一块内存地址的内容发生了改变,所以 a 也会体现出来,所以第三四个输出一样。当对 b 完全覆盖时,b 就指向了一块新的内存地址(并没有对原先的内存块作修改),a 还是指向原来的内存块,即 a 和 b 不再指向同一块内存,也就是说此时 a 和 b 已毫无关系,所以最后两个输出不一样。
明白了上述例子后,我们进入正题。
我们只需知道三点即可知道 exports 和 module.exports 的区别了:
exports是指向的module.exports的引用module.exports初始值为一个空对象{},所以exports初始值也是{}require()返回的是module.exports而不是exports
所以:
-
我们通过
var name = 'nswbmw'; exports.name = name; exports.sayName = function() { console.log(name); }给
exports赋值其实是给module.exports这个空对象添加了两个属性而已,上面的代码相当于:var name = 'nswbmw'; module.exports.name = name; module.exports.sayName = function() { console.log(name); } -
我们通常这样使用
exports和module.exports一个简单的例子,计算圆的面积:
使用 exports
app.js
var circle = require('./circle'); console.log(circle.area(4));circle.js
exports.area = function(r) { return r * r * Math.PI; }使用 module.exports
app.js
var area = require('./area'); console.log(area(4));area.js
module.exports = function(r) { return r * r * Math.PI; }上面两个例子输出是一样的。你也许会问,为什么不这样写呢?
app.js
var area = require('./area'); console.log(area(4));area.js
exports = function(r) { return r * r * Math.PI; }运行上面的例子会报错。这是因为,前面的例子中通过给
exports添加属性,只是对exports指向的内存做了修改,而exports = function(r) { return r * r * Math.PI; }其实是对
exports进行了覆盖,也就是说exports指向了一块新的内存(内容为一个计算圆面积的函数),也就是说exports和module.exports不再指向同一块内存,也就是说此时exports和module.exports毫无联系,也就是说module.exports指向的那块内存并没有做任何改变,仍然为一个空对象{},也就是说 area.js 导出了一个空对象,所以我们在 app.js 中调用 area(4) 会报TypeError: object is not a function的错误。所以,一句话做个总结:当我们想让模块导出的是一个对象时,
exports和module.exports均可使用(但exports也不能重新覆盖为一个新的对象),而当我们想导出非对象接口时,就必须也只能覆盖module.exports。 -
我们经常看到这样的用写法:
exports = module.exports = somethings上面的代码等价于
module.exports = somethings exports = module.exports原因也很简单,
module.exports = somethings是对module.exports进行了覆盖,此时module.exports和exports的关系断裂,module.exports指向了新的内存块,而exports还是指向原来的内存块,为了让module.exports和exports还是指向同一块内存或者说指向同一个 “对象”,所以我们就exports = module.exports。
“也就是说 exports 和 module.exports 不再指向同一块内存,也就是说此时 exports 和 module.exports 毫无联系,也就是说 module.exports 指向的那块内存并没有做任何改变,仍然为一个空对象 {} ,也就是说 area.js 导出了一个空对象,所以我们在 app.js 中调用 area(4) 会报 TypeError: object is not a function 的错误。”
这段话好多 也就是说。。。。
写的非常赞. 要是有图做个说明就最好了.
一开始我没有理解,原因是我没有明白nodejs 是怎么创建module.exports 和 exports,并且二者关系是怎么样的.
系统自动给nodejs 文件增加2个变量 exports 和 module, module 又有一个属性 exports, 这个exports 属性指向一个空对象 {}; 同时 exports这个变量也指向了这个空对象{};
于是就有了 exports => {} <=module.exports.
这2个exports 其实是没有直接关系的,唯一的关系是: 他们初始都指向同一个空对象{}; 如果其中一个不指向做个空对象了, 那么他们的关系就没有了.
懂的人看楼主的解释觉得很多余,不懂的人估计看得会比较迷糊,一个简单的例子: ~function(exports, module){ console.log(exports) //这里只要进行其它初始化的操作都会导致同样的效果 exports = {} console.log(exports) console.log(module.document) }(window.document, window)
前辈你好, 请问你的N-blog在heroku部署时 http://cuitianze.herokuapp.com/ 为什么会出现如下的问题呢
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.
另外,应该不是部署的问题,因为你的另一个开源作品N-chat能够部署成功 http://tianze.herokuapp.com/
“module.exports = somethings 是对 module.exports 进行了覆盖,此时 module.exports 和 exports 的关系断裂,module.exports 指向了新的内存块,而 exports 还是指向原来的内存块” 前面不是说 exports 是指向 module.exports 的引用吗?为什么对 module.exports 赋新值会打断 module.exports 和 exports 的关系?