核心提示:module.exports和exports的区别通常来讲, 我们在node模块中输出变量有两种方法.例一. 采用module.exportsfunction hello() {console.log...
module.exports和exports的区别
通常来讲, 我们在node模块中输出变量有两种方法.
例一. 采用module.exports
function hello() { console.log('Hello, world!'); } function world(name) { console.log('Hello, ' + name); } module.exports = { hello: hello, world: world };
例二. 采用exports
function hello() { console.log('Hello, world!'); } function world(name) { console.log('Hello, ' + name); } exports.hello = hello; exports.world= world;
区别
不可以直接对进行exports赋值:
exports = { hello: hello, world: world };//这样代码不报错, 但是没有用, 接口并未暴露出来. 原因请看下面
原因
首先,Node会把整个待加载的 .js文件放入一个包装函数load中执行。在执行这个load()函数前,Node事先准备好了module对象. module中有一个空的exports方法.
var load = function (exports, module) { // .js的文件内容 ... // load函数返回: return module.exports; }; var exported = load(module.exports, module);
那么为什么在自己的 .js 文件中写成exports = { … }会没有效果呢 ?
系统自动给nodejs 文件增加2个变量 exports 和 module, module 又有一个属性 exports, 这个exports 属性指向一个空对象 {}; 同时 exports这个变量也指向了这个空对象{};
于是就有了 exports => {} <=module.exports.
这2个exports 其实是没有直接关系的,唯一的关系是: 他们初始都指向同一个空对象{};
如果其中一个不指向做个空对象了, 那么他们的关系就没有了. 因此也就没有任何效果了.
总结
因此如果是想要键值对象的这种格式, exports.text = …; 是可以的.
但如果是一个数组或者是函数. 只有module.exports = function(){ }有效.
因此, 不论任何时候都推荐用module.exports