56

What I mean is does node.js have object that are global function methods of. Like this in browser:

function myGlobalFunction() {
    console.log(this === window);
}
myGlobalFunction();

=> true
IGRACH
  • 3,036
  • 4
  • 26
  • 40

2 Answers2

50

The closest equivalent in node is global. I'm not sure if it translates in all of the same ways, but if you open a REPL and type in this === global, it will return true.

Here's a discussion on the global object, though some it the information may be deprecated as it's pretty old: 'Global' object in node.js

Community
  • 1
  • 1
EmptyArsenal
  • 6,518
  • 3
  • 28
  • 51
  • 3
    this === global will only return true inside of a REPL. If you are actually executing a script file like node myScript.js it will be false. See this post for an explanation http://stackoverflow.com/questions/25336759/global-variable-assignment-in-node-from-script-vs-command-line – Chris Wininger Mar 06 '15 at 16:38
22

Yes, the global variable is the global object in Node.js

From the docs:

global# {Object} The global namespace object. In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

plalx
  • 39,329
  • 5
  • 63
  • 83