问题描述
我在Node.js模块中找到了以下合约:
I've found the following contract in a Node.js module:
module.exports = exports = nano = function database_module(cfg) {...}
我想知道之间的差异是什么module.exports
和 exports
以及为什么在这里使用两者。
I wonder whats the different between module.exports
and exports
and why both are used here.
推荐答案
设置 module.exports
允许在<$时调用 database_module
函数C $ C>需要。简单地设置 exports
将不允许该函数导出
,因为节点导出对象 module.exports
引用。以下代码不允许用户调用该函数。
Setting module.exports
allows the database_module
function to be called like a function when required
. Simply setting exports
wouldn't allow the function to beexported because node exports the object module.exports
references. The following code wouldn't allow the user to call the function.
以下内容无效。
exports = nano = function database_module(cfg) {return;}
如果 module.exports
已设置,以下内容将有效。
The following will work if module.exports
is set.
module.exports = exports = nano = function database_module(cfg) {return;}
控制台
var func = require('./module.js');
// the following line will **work** with module.exports
func();
基本上 node.js 不会导出<$ c的对象$ c> exports 当前引用,但导出 exports
最初引用的属性。虽然 Node.js 会导出对象 module.exports
引用,但允许您像函数一样调用它。
Basically node.js doesn't export the object that exports
currently references, but exports the properties of what exports
originally references. Although Node.js does export the object module.exports
references, allowing you to call it like a function.
他们设置 module.exports
和 exports
以确保 exports
未引用先前导出的对象。通过设置你使用 exports
作为速记,并避免在未来的潜在错误。
They set both module.exports
and exports
to ensure exports
isn't referencing the prior exported object. By setting both you use exports
as a shorthand and avoid potential bugs later on down the road.
使用 exports.prop = true
而不是 module.exports.prop = true
保存字符并避免混淆。
Using exports.prop = true
instead of module.exports.prop = true
saves characters and avoids confusion.
这篇关于node.exports与Node.js中的导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!