module.export与export的区别?

对于大多数node初学者而言, module.exports应该都是理解的, 但多出来一个exports获取就有些疑问了

 

疑问一: 既然有module.exports了为什么还要有exports?

疑问二: 两者有什么区别?

 

首先, 官网是这么回答的

  The exports variable is available within a module's file-level scope, and is assigned the value of module.exports before the module is evaluated.

  It allows a shortcut, so that module.exports.f = ... can be written more succinctly as exports.f = ...

也就是说, exports相当于一个快捷方式,exports.f = ....  肯定是比 module.exports.f = ... 写起来方便一些。下面附上一段express源码中的使用你就明白了。

exports = module.exports = createApplication;

exports.mime = connect.mime;
exports.application = proto;
exports.request = req;
exports.response = res;

function createApplication() {
  var app = connect();
  merge(app, proto);
  app.request = { __proto__: req, app: app };
  app.response = { __proto__: res, app: app };
  app.init();
  return app;
}

其实exports是将指针执行module.exports对象, 如果你像exports = function(){}这样相当于改变了exports原来的指向, 也就无法被导出, 为什么?先看官网给的类似实现:

function require(/* ... */) {
  const module = { exports: {} };
  ((module, exports) => {
    // 你的模块代码在这。在这个例子中,定义了一个函数。
    function someFunc() {}
    exports = someFunc;
    // 此时,exports 不再是一个 module.exports 的快捷方式,
    // 且这个模块依然导出一个空的默认对象。
    module.exports = someFunc;
    // 此时,该模块导出 someFunc,而不是默认对象。
  })(module, module.exports);
  return module.exports;
} 

根据上面的代码可以看出exports是模块内部一个形参对象, 如果给exports对象添加属性是可以导出的, 因为指针并未改变, 但如果赋值一个对象就不行了, 因为指针已经改变了,最后导出的是module.exports

 

转载于:https://www.cnblogs.com/zhen-rh/p/6926287.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值