Why is the "this" an empty object in NodeJS? Where does the "module.exports" belongs to?
I thought that "module.exports..."
could be written as "this.module.exports..."
but it won't work.
Thank you
If you are in an actual module:
module
is a reference to the current module
exports
is a reference to the exported data. It gets cached away by NodeJS and delivered to other modules that require
it
this
is an alternate reference to the exports
object
module.exports
is an alternate reference to the exports
object
this.module
is undefined
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.
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.
If you are in the REPL, then this.module
is defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With