3

I know in node, every module gets an local scope and variable defined within one module won't escape to the global unless explicitly exported.

What I want to know is when I declare a variable in one module file as the following, what's the global object this variable defined on?

var obj = {};
// In browser, this is defined on window.obj
// How about in the node?

There is one statement that global is the object used as the local global scope, however the following test code fails:

a = 1
console.log global.a
// undefined

So what is the global variable for a module?

Community
  • 1
  • 1
steveyang
  • 9,178
  • 8
  • 54
  • 80
  • If you're working in a new (post 0.8 maybe) version of Node, then "global" seems to work. In 0.6.2, which is what I seemed to get via Ubuntu 12.04, it doesn't work. – Pointy Jun 30 '12 at 19:39
  • I've not actually had a chance to play around much with Node.js yet.. still waiting for some downtime.. :) but just out of interest what happens if you grab the value returned by the following code? `var glob = (function(){return this;})();` - in the world of browser based JS this would return the Window object for most environments. – Pebbl Jul 01 '12 at 01:06
  • @Pointy I am using `0.8.1` and `0.6.19` and both fails the above code. – steveyang Jul 01 '12 at 15:43
  • Hmm well I don't thoroughly "get" Node; I cloned the git repository and that's apparently "0.9.0-pre" or something like that. – Pointy Jul 01 '12 at 16:42

2 Answers2

1

There is in fact a global object in node; it's simply called global. You can assign properties to it and access those properties just like any other object, and you can do it across modules. However, directly accessing it (global.foo=bar in one module, baz=global.foo in another) is the only way to access it. Unqualified variable names never automatically resolve to its properties the way that, say, an unqualified variable name in a browser environment would resolve to window.somethingOrOther. In node, every module is conceptually wrapped inside an immediate function call, so unqualified names represent local variables and have module-level scope.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
0

edit again I'm just about ready to conclude that, in a Node module, you can't get a reference to the global object. You really shouldn't need to; that's kind-of the whole point of the module mechanism, I think. You import what you need and export what you choose.

Whether there even is a global object in Node is an interesting question, I guess. I know that in Rhino, there definitely is; there's no implicit wrapper function around code fed to Rhino from the Java container. Via the Java ScriptEngine mechanism (and presumably from Mozilla APIs in "naked" Rhino) there are ways of pushing symbols into the global context to make them visible to JavaScript as global object properties.

wow this got complicated well things seem to be on the move in the Node.js world. What I wrote above was true for Node 0.6.2, but in the 0.9.0-pre build I just did there is indeed an object called "global" that behaves more or less like the global object in a browser.


The stuff below applies to browsers and Rhino and other contexts like that

You can use this in the global context. If you need a name, you can give it one.

var global = this;

var obj = "Hi!";

global.obj = "Bye"; // sets "obj"

A (somewhat) common idiom is to wrap your code in a function, to protect the global namespace.

(function( global ) {
  // everything
})( this );

Caveat: I'm not a Node person so there may be some emerging idiom in that culture.

edit — it occurs to me that if Node really does wrap code from files it evaluates in a function, and it doesn't pass this into it from the global context, then there's no way to "find" it, I don't think. If you use "use strict" (and you should), it doesn't matter, because you can't really mess with the global context anyway.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 2
    There is no standard global variable setup already? I know node automatically wrap your module in `(function () {})()` to protect the code from polluting the global scope. – steveyang Jun 30 '12 at 17:55
  • As I said, I'm not a Node person so I can't say for sure. You could look for properties of the global object whose value is the same object, and then check for a name :-) – Pointy Jun 30 '12 at 17:55
  • This may be the thing that makes me actually try playing with Node :-) – Pointy Jun 30 '12 at 17:57