栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Node.js中的module.exports与export

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
保存字符并避免混淆。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号