Following JS code executes an anonimous function and defines a function met() and makes it visible from the global context:
(function(GLOB){
GLOB.met = function(s) {
console.log(s);
}
})(this);
met("Hi!");
When run in a browser, works fine and displays "Hi!" on the console. However, when run in Node.js, the last line throws the error "met is not defined".
When I now change the last line in:
this.met("Hi!");
it works well on Node.js as well.
Why the first version does not work on Node?
"This" is not global in the outerscope of nodejs. Instead use global.varName.
The best practice is to use GLOBAL.VAR_NAME so that you know not to do it often.
If you want some consistency with the browser I'd recommend browserify as it let's you use global on the client side, as well as require and even merges your js for you! Just my two cents.