node.js中exports与module.exports的区别分析

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

内容简介:关于Node.js中的exports和module.exports,很多时候都比较容易让人混淆,弄不清楚两者间的区别。那么我们就从头开始理清这两者之间的关系。在开发Node.js应用的时候,很多模块都是需要引入才能使用,但是为什么exports和module.exports我们没有引用却可以直接使用呢?事实上,Node.js应用在编译的过程中会对JavaScript文件的内容进行头尾的封装。例如:

关于Node.js中的exports和module.exports,很多时候都比较容易让人混淆,弄不清楚两者间的区别。那么我们就从头开始理清这两者之间的关系。

来源

在开发Node.js应用的时候,很多模块都是需要引入才能使用,但是为什么exports和module.exports我们没有引用却可以直接使用呢?

事实上,Node.js应用在编译的过程中会对JavaScript文件的内容进行头尾的封装。例如:

// hello.js
const hello = function () {
	console.log('Hello world');
}
module.exports = {
	hello
}
// 头尾封装后的js代码
(function (exports, require, module, __filename, __dirname) {
    const hello = function () {
        console.log('Hello world');
    }
    module.exports = {
        hello
    }
})
复制代码

在进行了头尾封装之后,各个模块之间进行了作用域隔离,避免了污染全局变量,同时可以使每个模块在不引入这些变量的情况下可以使用它们。这些变量依次为当前模块的exports属性、require()方法、当前模块自身(module)、在文件系统中的完整路径、文件目录。

区别

按照Node.js的解释,exports是module对象的一个属性,那么exports和module.exports应该是等价的。的确如初,初始化的exports和module.exports变量的值均为{},代码验证:

// hello.js
const hello = function () {
    console.log('Hello world');
}
console.log('初始值==========');
console.log(exports);
console.log(module.exports);
module.exports = {
    hello
}
// 输出结果
初始值==========
{}
{}
复制代码

可以发现,module对象的exports属性和exports均指向一个空对象{},那么在导出对象的时候使用exports和module.exports有什么区别呢?

我们在使用require()方法引入模块的时候,其实是引入了module.exports对象, exports只是module对象的exports的一个引用,我们可以通过修改exports所指向对象的值来协助修改module.exports的值。

  • 使用exports导出
const hello = function () {
    console.log('Hello world');
}
exports.hello = {
    hello
}
console.log('修改值==========');
console.log(exports);
console.log(module.exports);
// 输出结果
修改值==========
{ hello: { hello: [Function: hello] } }
{ hello: { hello: [Function: hello] } }
复制代码

由于exports和module.exports指向同一块内存区域,所以我们修改exports对象的数据,那么module.exports也会随之改变。

  • 使用module.exports导出
// hello.js
const hello = function () {
    console.log('Hello world');
}
module.exports = {
    hello
}
console.log('修改值==========');
console.log(exports);
console.log(module.exports);
// 输出结果
修改值==========
{}
{ hello: [Function: hello] }
复制代码

你会发现修改后的exports依然是{},而module.exports的值已经改变,这是由于当你给module.exports是直接等于一个新的对象,那么其将指向一块新的内存区域,而此时exports指向的仍然是之前的内存区域,所以二者的值会不一样,但是此时你在其他文件内引入hello.js文件,仍然可以调用hello()方法,这也说明了导出的是module.exports而不是exports。

  • 给exports直接赋值
// hello.js
const hello = function () {
    console.log('Hello world');
}
exports = {
    hello
}
console.log('修改值==========');
console.log(exports);
console.log(module.exports);
// 输出结果
修改值==========
{ hello: [Function: hello] }
{}
复制代码

使用这种方法导出在其他文件调用hello方法即会报错,因为该文件模块导出的对象为空,当然也不可能有hello()方法,这种问题的原因同样是指向的内存区域发生变化所导致的。

总结

  1. exports对象是module对象的一个属性,在初始时exports和module.exports是指向同一块内存区域的;
  2. 在不改变exports内存指向的情况下,修改exports的值可以改变module.exports的值;
  3. 导出尽量使用module.exports以避免混淆。

以上所述就是小编给大家介绍的《node.js中exports与module.exports的区别分析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

One Click

One Click

Richard L. Brandt / Portfolio Hardcover / 2011-10-27 / 25.95

An insightful look at how Amazon really works and how its founder and CEO makes it happen. Amazon's business model is deceptively simple: make online shopping so easy and convenient that customers ......一起来看看 《One Click》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换