exports 与 module.exports 的区别

exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用。

那么require()一个模块时,到底做了什么呢?下面大概展现了require()的伪代码实现:

function require(/* ... */) {
  const module = { exports: {} };
  ((module, exports) => {
    // Module code here. In this example, define a function.
    // function someFunc() {}
    // exports = someFunc;
    // At this point, exports is no longer a shortcut to module.exports, and
    // this module will still export an empty default object.
    // module.exports = someFunc;
    // At this point, the module will now export someFunc, instead of the
    // default object.
  })(module, module.exports);
  return module.exports;
}

由此可以看出:

  • module.exports默认指向一个空对象{}
  • exports在默认情况下是指向module.exports的引用
  • require()返回的是module.exports而不是exports

所以,如果是通过exports.test = function() {} 的形式暴露出去,无论是使用exports还是使用module.exports,在功能上并没有什么差别;但是如果是通过exports = function() {} 的形式暴露出去,结果往往会出乎意料。从伪代码中可知,exports对象是通过形参的方式传入的,直接赋值形参会改变形参的引用,但是不会改变作用域外的值,也就是说module.exports原来的值是什么,现在还是什么,不会有任何改变,而require()方法最终返回的是module.exports,所以结果可想而知。

// 文件1.js
exports = function () {
    console.log('exports')
}

// 文件2.js
var test = require('./1');
console.log(test); // 结果是module.exports的默认值,也就是空对象{}

转载于:https://www.cnblogs.com/philipding/p/7841517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值