2

I have a simple codes like the following and execute it as a node module:

console.log(this);
module.exports = {…};

I know that global is the default context (like window in browsers), but what does the this keyword refer to?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
yanni4night
  • 137
  • 7
  • See also the related topics [Meaning of `this` in node.js modules and functions](http://stackoverflow.com/q/22770299/1048572) and [Difference between `this` in node repl and in module](http://stackoverflow.com/q/20861049/1048572) – Bergi Mar 02 '16 at 16:56
  • For details about node module internals, see [this question](http://stackoverflow.com/q/28955047/1048572) – Bergi Mar 02 '16 at 17:00

1 Answers1

5

this (in the context of a module) is the same as exports in node.js. However you should generally use exports/module.exports instead, so that it's explicitly clear what you're modifying.

mscdex
  • 104,356
  • 15
  • 192
  • 153