问题描述
限时送ChatGPT账号..为什么 NodeJS 中的this"是一个空对象?module.exports"属于哪里?
Why is the "this" an empty object in NodeJS? Where does the "module.exports" belongs to?
我认为 "module.exports..."
可以写成 "this.module.exports..."
但它不起作用.
I thought that "module.exports..."
could be written as "this.module.exports..."
but it won't work.
谢谢
推荐答案
如果您在实际模块中:
module
是对当前模块的引用
exports
是对导出数据的引用.它被 NodeJS 缓存起来并传递给需要
它
exports
is a reference to the exported data. It gets cached away by NodeJS and delivered to other modules that require
it
this
是对 exports
对象的替代引用
this
is an alternate reference to the exports
object
module.exports
是对 exports
对象的替代引用
module.exports
is an alternate reference to the exports
object
this.module
是 undefined
this
是空的,因为如上所述,它是对与 exports
相同的对象的引用,该对象将由开发人员填充.
this
is empty because as noted above it is a reference to the same object as exports
, which is to be populated by the developer.
如果this.module
是对module
的引用,那么由于this
是对exports
的引用,它会将 module
本身与其他导出的项目一起导出.我怀疑这是需要的.
If this.module
was a reference to module
, then since this
is a reference to exports
, it would export the module
itself along with the other exported items. I doubt this would be desired.
如果您在 REPL 中,则定义了 this.module
.
If you are in the REPL, then this.module
is defined.
这篇关于NodeJS“这个"空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论