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

NodeJS / Express中的“ module.exports”和“ exports.methods”是什么意思?

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

NodeJS / Express中的“ module.exports”和“ exports.methods”是什么意思?

更具体:

module
是文件内的全局范围变量。

因此,如果您致电

require("foo")

// foo.jsconsole.log(this === module); // true

它的行为与

window
在浏览器中的行为相同。

还有一个称为的全局对象

global
,您可以在所需的任何文件中进行读写,但这涉及到更改全局范围,这就是 EVIL

exports
是存在的变量
module.exports
。基本上,这是您在需要文件时 导出 的内容。

// foo.jsmodule.exports = 42;// main.jsconsole.log(require("foo") === 42); // true

单独存在一个小问题

exports
。在_global范围上下文+和
module

一样的。(在浏览器中,全局范围上下文与之
window
相同)。

// foo.jsvar exports = {}; // creates a new local variable called exports, and conflicts with// living on module.exportsexports = {}; // does the same as abovemodule.exports = {}; // just works because its the "correct" exports// bar.jsexports.foo = 42; // this does not create a new exports variable so it just works


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

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

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