i.e. what’s the equivalent of the browser’s “window” object, to which top level functions are attached?
The following code succeeds in the REPL:
var assert = require('assert');
function foo() { };
assert.ok(foo == this["foo"]);
However in a script (or a module) it fails–in both cases “this” is an empty object.
I’m wondering about this so I can easily export all functions visible in a module’s namespace–I want to be able to do something like:
function foo() { };
function bar() { };
["foo", "bar"].forEach(function (k) {
exports[k] = ???;
});
(eval(k) works for the ???, but, ugh.)
Matt Ball’s answer is pretty good:
Alternatively,
Some modules which exhibit best practices:
https://github.com/cloudhead/journey/blob/master/lib/journey.js
For browser compat: https://github.com/caolan/async/blob/master/lib/async.js
https://github.com/mikeal/request/blob/master/main.js
/fyi #node.js IRC welcomes you: http://bit.ly/nodeIRC