module.exports 还是 exports?
来源:互联网 发布:暖风机哪个牌子好 知乎 编辑:程序博客网 时间:2024/06/07 18:11
转载自:http://zihua.li/2012/03/use-module-exports-or-exports-in-node/
Node.js 通过实现 CommonJS 的 Modules/1.0 标准引入了模块(Module)概念,一个模块可以通过 module.exports
或 exports
将函数、变量等导出,以使其它 JavaScript 脚本通过 require()
函数引入并使用。
现在有一个问题:到底应该用 module.exports
还是用 exports
呢?
最好的方法就是查看 Node.js 的官方文档中关于module的介绍:
In particular module.exports is the same as the exports object.
可以看出 module.exports
和 exports
其实是一样的。在代码里比较:
console.log(module.exports === exports);
输出结果也是 true
。
然而参照 "module.exports vs exports in nodeJS" 中的例子,考虑下面的代码:
// a1.jsexports = function () { console.log('hi a2!');}:::javascript// a2.jsmodule.exports = function () { console.log('hi a2!');}:::javascript// b.jsconsole.log(require('./a1'));console.log(require('./a2'));
执行node b,输出结果如下:
{}[Function]
可见 a1.js 的 function 导出失败了,而 a2 却成功了。原因是什么呢?搜索无果后我直接查看了Node.js 的源代码:
// file path: node/lib/module.js// ...global.require = require;global.exports = self.exports;global.__filename = filename;global.__dirname = dirname;global.module = self;return runInThisContext(content, filename, true);// ...
这段代码的作用是为 global
赋属性并调用 runInThisContext()
函数运行模块(其内容已经事先读入到 content
变量)。模块中使用的 exports
(即 global.exports
)其实是对 self.exports
的引用,对 exports
进行修改会影响到 self.exports
,但如果对其赋值,exports
则不再是 self.exports
的引用了。而对 module.exports
赋值自然就没有问题。
了解这个,有些 Node.js 模块的源代码中的 exports = module.exports = someObject
也可以理解了:只使用 module.exports = someObject
的话 module.exports
引用的地址变了,而 exports
引用的还是之前的地址,所以下面的代码中再修改 exports
就不会再改变 module.exports
的值了。
总结
养成好习惯,如果不给 exports
本身赋值的话就用 exports.sth = sobj
,如果要赋值的话则这样写:
exports = module.exports = someObject;exports.sth = sobj; // 如果还需要加入其他属性则这样写
更多阅读
- CommonJS Modules/1.0 CommonJS 的成果之一就是为 JavaScript 加入了 module 概念,Node.js 实现了其中 Modules/1.0 标准。在这个网站上可以看到其它实现该标准的项目,看看你写的模块还可以在哪个项目里用。
- Exports vs module.exports 为数不多的讨论此问题的文章,讲得比较清楚,只是对 module.exports 和 exports 的关系没有说到本质。
- module.exports 还是 exports?
- module.exports还是exports?
- exports VS module.exports
- exports module.exports
- exports 和module.exports
- exports和module.exports
- exports和module.exports
- exports 和 module.exports
- module.exports和exports
- module.exports与exports
- module.exports和exports
- Nodejs exports与module exports
- Nodejs exports与module exports
- 细说exports和module.exports
- [nodejs]exports和module.exports
- 关于module.exports与exports
- module.export and exports
- exports,module对象
- hdu 4289 Control(网络流 最大流+拆点)(模板)
- ACM练级日志: POJ 1376
- 关于Android ViewPager与资源文件的矛盾关系
- EXC_ARM_DA_ALIGN问题
- Spark&Spark性能调优实战
- module.exports 还是 exports?
- quick-cocos2dx mac android 编译真机调试
- ACM练级日志:微软编程之美比赛测试赛-3(高精度相关)
- Accounting Flexfield Setup and Usage (Doc ID 124333.1)
- Python学习笔记01 从零开始Hello world
- android网络时间同步总结
- c++头文件使用规则
- ACM练级日志:模板的使用
- win7网络发现不能启用,无法共享其他机器的解决办法