Node.js中的module.exports与export

Node.js中的module.exports与export,第1张

Node.js中的module.exports与export

设置

module.exports
允许在
database_module
时像函数一样调用函数
required
。简单地设置
exports
将不允许导出函数,因为节点导出了对象
module.exports
引用。以下代码不允许用户调用该函数。

module.js

以下内容无效。

exports = nano = function database_module(cfg) {return;}

如果

module.exports
设置以下内容,则将起作用。

module.exports = exports = nano = function database_module(cfg) {return;}

安慰

var func = require('./module.js');// the following line will **work** with module.exportsfunc();

基本上, node.js 不会导出

exports
当前引用的对象,而是导出
exports
最初引用的对象的属性。尽管 Node.js
确实导出了对象
module.exports
引用,但允许您像调用函数一样调用它。


第二个最不重要的原因

他们设置了两者

module.exports
exports
确保
exports
未引用先前的导出对象。通过将两者都设置
exports
为简写,可以避免以后出现潜在的错误。

使用

exports.prop = true
而不是
module.exports.prop = true
保存字符并避免混淆。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5021940.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存