Node.js 之 module.exports 和 exports

栏目: Node.js · 发布时间: 7年前

内容简介:Node.js 模块系统是采用 CommonJS 模块规范的。每个文件视为一个独立的模块。使用 require 导入模块,使用 module.exports 和 exports 导出模块。那么 module.exports 和 exports 的区别在哪里呢?

Node.js 之 module.exports 和 exports

Node.js 模块系统是采用 CommonJS 模块规范的。每个文件视为一个独立的模块。使用 require 导入模块,使用 module.exports 和 exports 导出模块。

那么 module.exports 和 exports 的区别在哪里呢?

module.exports

  1. module.exports 就是 require() 的返回值
  2. module.exports 是模块系统自动创建的,且初始化为空对象 {}

exports

  1. exports 是为了方便快捷创建的变量,指向 module.exports 的引用

说明

可以看一下 Node 文档 中的一段解释

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 = module.exports = {...}

module.exports 是一个对象,exports 是对 module.exports 的引用,即他们指向同一块内存。如图所示:

Node.js 之 module.exports 和 exports

所以如果对 exports (或者 module.exports) 的对象修改,就是对他们共同指向的内存的内容做修改,两者都会影响。

// 这样是可以的
exports.obj = 1;
// or
module.exports.obj = 1;

但是如果直接将 exports (或者 module.exports) 指向一个值,则会使 exports (或者 module.exports) 指向新的内存块,等于断开了 exports 和 module.exports 的联系。下面的两种情况,导出的值要看 module.exports 的值

// 直接赋值 exports 是无效的,导出的模块就不是 exports 的值了
exports = function(x) {console.log(x)};
// 直接赋值 module.exports 也会导致 exports 的值无法导出
exports.obj = 1;
module.exports = 2;

Node.js 之 module.exports 和 exports

这时候,我们可以 exports = module.exports 让 exports 重新指向 module.exports

总结

对于 module.exports 和 exports 我们只需要记住三点就行了:

  1. module.exports 是模块系统自动创建的,且初始化为空对象 {}
  2. require() 返回的是 module.exports 的值
  3. exports 指向 module.exports 的引用

在使用中,建议使用 module.exports 来导出模块,这样可以应对所有情况。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Ajax for Web Application Developers

Ajax for Web Application Developers

Kris Hadlock / Sams / 2006-10-30 / GBP 32.99

Book Description Reusable components and patterns for Ajax-driven applications Ajax is one of the latest and greatest ways to improve users’ online experience and create new and innovative web f......一起来看看 《Ajax for Web Application Developers》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码