Nodejs中exports和module.exports的区别
Nodejs中exports和module.exports的区别
exports.start = start; 这个我就懂得了, exports.Manager = require(’./manager’); 这句我就不懂得了,
还有module.exports和exports有什么区别啊,很多时候看他们这样写 exports = module.exports = Manager;
Node.js 中 exports
和 module.exports
的区别
在 Node.js 中,exports
和 module.exports
都用于导出模块中的对象、函数或变量。但它们之间有一些重要的区别。
exports
和 module.exports
的基本概念
exports
: 这是一个指向module.exports
的引用。默认情况下,它是一个空对象{}
。module.exports
: 这是 Node.js 模块系统用来定义模块的公共接口的对象。
示例代码解释
假设你有一个名为 manager.js
的文件,内容如下:
// manager.js
class Manager {
constructor(name) {
this.name = name;
}
start() {
console.log(`Manager ${this.name} started.`);
}
}
module.exports = Manager;
在这个例子中,我们直接将 Manager
类赋值给 module.exports
,这意味着该模块的导出对象就是 Manager
类。
现在,让我们看看另一个文件 app.js
如何使用这个模块:
// app.js
const Manager = require('./manager');
const manager = new Manager('John');
manager.start();
在这个例子中,require('./manager')
返回的就是 module.exports
导出的对象,即 Manager
类。
exports
和 module.exports
的区别
-
exports
是module.exports
的引用:-
如果你在文件中只使用
exports
来添加属性,那么这些属性会附加到module.exports
对象上。 -
例如:
// exports.js exports.start = function() { console.log("Start function"); }; exports.Manager = require('./manager');
-
-
直接赋值给
module.exports
:-
如果你直接将一个对象赋值给
module.exports
,那么exports
将不再指向module.exports
,而是变成一个空对象。 -
例如:
// module.exports.js module.exports = { start: function() { console.log("Start function"); }, Manager: require('./manager') };
-
-
同时赋值给
exports
和module.exports
:-
你可以将一个对象同时赋值给
exports
和module.exports
,这样可以确保两个对象都指向同一个引用。 -
例如:
// exports-and-module-exports.js exports = module.exports = Manager;
-
总结
- 使用
exports
可以方便地向module.exports
添加属性。 - 直接赋值给
module.exports
会改变exports
的引用,使其指向一个新的空对象。 - 同时赋值给
exports
和module.exports
可以确保两者指向同一个对象。
理解这些差异可以帮助你更好地组织和管理你的 Node.js 模块。
- exports.Manager = require(’./manager’); 是先引入’./manager’这个module,它可能是一个目录或文件,看你的实现而定,然后将它赋给Manager,在export出去共其它模块require(注意,require的其实是module.exports)
- 在一个模块(如nodejs文件)中,如果一开始没有做任何操作,那么exports===module.exports,也就是说exports是指向的module.exports的引用。
- 我觉得两个还是不要混用的好,忘记了就容易出现问题,我一般都这样用,exports.=
这篇文章貌似说得很清楚了~
Here is an eye-opener - module.exports is the real deal. exports is just module.exports’s little helper. Your module returns module.exports to the caller ultimately, not exports. All exports does is collect properties and attach them to module.exports IF module.exports doesn’t have something on it already. If there’s something attached to module.exports already, everything on exports is ignored.
So you get the point now - if you want your module to be of a specific object type, use module.exports; if you want your module to be a typical module instance, use exports.
Having said that, exports is the recommended object unless you are planning to change the object type of your module from the traditional ‘module instance’ to something else.
E文不好,就不翻译了,直接贴上重点~~
每个模块在被require的时候都被做了头尾包装。这个方法执行后,将exports对象返回给引用方。你可以看到exports是通过参数传递进来的。如果exports整个引用被改了,那么返回的将是原来的对象。所以这个时候需要module.exports来代替。
(function (exports, require, module, __filename, __dirname) {
var circle = require('./circle.js');
console.log('The area of a circle of radius 4 is ' + circle.area(4));
});
exports指向module.exports,而且require方法返回的就是module.exports。所以我就奇怪了为什么还需要exports,直接用module.exports不就行了吗
Node.js 中 exports
和 module.exports
的区别
在 Node.js 中,exports
和 module.exports
都是用来导出模块的功能,但它们之间有一些重要的区别。
1. exports
是一个指向 module.exports
的引用
exports
是module.exports
的引用。- 默认情况下,
exports
指向module.exports
。 - 你可以直接修改
exports
来添加新的属性或方法,也可以直接修改module.exports
。
2. 修改 exports
和 module.exports
- 如果你在代码中使用
exports.someFunction = function() {}
,这是合法的。 - 但是,如果你尝试将
exports
重新赋值为一个新的对象(如exports = { ... }
),这不会改变module.exports
。因为exports
只是module.exports
的一个引用,而不是module.exports
本身。
示例代码
// 假设这是 myModule.js 文件
// 正确的方式
exports.someFunction = function() {
console.log('Hello, world!');
};
// 这样写会覆盖默认的 exports 引用,导致无法正常导出
// exports = {
// anotherFunction: function() {
// console.log('Another function');
// }
// };
// 正确的方式
module.exports = {
anotherFunction: function() {
console.log('Another function');
}
};
为什么 exports.Manager = require('./manager');
可以工作?
在这个例子中,exports.Manager = require('./manager');
实际上是将 require('./manager')
的返回值赋给 module.exports.Manager
。
// 假设这是 myModule.js 文件
exports.Manager = require('./manager');
// 等价于
module.exports.Manager = require('./manager');
为什么 exports = module.exports = Manager;
可以工作?
这里 exports
被重新赋值为 module.exports
,因此 exports
和 module.exports
指向同一个对象。然后 Manager
对象被赋值给这个共同的对象。
// 假设这是 myModule.js 文件
function Manager() {
console.log('Manager instance created.');
}
exports = module.exports = Manager;
// 等价于
module.exports = Manager;
exports = Manager;
通过这些示例,你可以看到 exports
和 module.exports
在实际使用中的区别和正确的使用方式。