3

What object does this reference in module scope (i'm referring to node.js modules)?

console.log(this);
console.log(this === module); //false

It isn't referencing to module object so where is it pointing to?

Kaarel Purde
  • 1,145
  • 3
  • 14
  • 30

1 Answers1

3

Strangely, it's equal to module.exports

console.log(this === module.exports); // => true

Personally, I think this is dumb. But it is what it is.

I have no idea why we have 3 references to the same exact thing. this, exports, and module.exports all reference the same object.

My best guess is it's some backwards compatibility. I could easily be wrong about that tho.

Thank you
  • 107,507
  • 28
  • 191
  • 224