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