详解Node.js中exports和module.exports的区别

2022-01-14,,,,

这篇文章主要介绍了详解Node.jsexports和module.exports的区别,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

今天看了下node.js的require方法的源码,终于搞清楚exports和module.exports的区别了。

我们知道,node.js的模块暴露有两种方法。

1. 方式一:用exports

 //a.js exports.log =function (str) { console.log(str); } 
 //b.js var s = require("./a"); s.log("哈哈哈哈"); 

2. 方式二:用module.exports

 //a.js module.exports = function (str) { console.log(str); } //b.js var s = require("./a"); s("嘻嘻嘻嘻"); 

如果将第一种方式的exports按照第二种方式写成下面这样就会出错:

 //a.js exports = function (str) { console.log(str); } //b.js var s = require("./a"); s("哈哈哈哈"); 

exportsmodule.exports的初始值指向的是空对象,即{}。从源码可以看到,其实模块的require方法实质上是调用了_load方法,而_load方法,最终返回的是module.exports

 

来分析一下出错的原因。

由于最开始的时候,exportsmodule.exports都指向同一个对象。

第一种方式,是在给这个空对象{}添加属性,又因为module.exports也是指向这个对象的,所以最终require方法返回的module.exports是指向了这个具有log方法的对象的,可以引用到模块。

第二种方式是让module.exports指向一片新的内存空间,exports指向的仍然是{},但是由于require方法返回的是module.exports,所以最终也能引入模块。

但是最后那种写法是让exports指向一片新的内存空间,module.exports指向的仍然还是{},那么最终require方法是将module.exports返回,所以会导致报错,说s不是一个function。

所以:

请牢记:require方法返回的是module.exports

以上就是详解Node.js中exports和module.exports的区别的详细内容,更多请关注本站其它相关文章!

《详解Node.js中exports和module.exports的区别.doc》

下载本文的Word格式文档,以方便收藏与打印。