1

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
  • 513,640
  • 108
  • 821
  • 1,164
yanni4night
  • 127
  • 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

4

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
  • 93,083
  • 13
  • 170
  • 135