Sikorski
Sikorski

Reputation: 2691

Nodejs: Wrapping entire script in a function call

I have been writing modules in nodejs as following :

module.exports = function (logger, db, external,constants) {

        return {
           //something

        }
    }

Recently someone in my team suggested that whole script should be wrapped in a function to avoid global confusion of variables i.e. like this :

(function () {
    'use strict';
    module.exports = function (logger, db, external,constants) {

        return {
               //something
        }
    }

}());

I understand that this practice is generally used at client side code. But during server side in nodejs is this required? I think that in nodejs there is really no global scope and only module.exports is the one which is accessible really irrespective of whatever we write in script file (ofcourse don't go wild over here).

Upvotes: 6

Views: 3353

Answers (2)

slebetman
slebetman

Reputation: 113906

You're actually already doing this.

What he's suggesting is to wrap the entire script in a function like this:

function () {

}

That's all. Nothing special. Of course, in regular javascript a function definition just defines a function and the code inside the function doesn't run. So to automatically run the function you wrap it in an expression context and invoke it:

(function () {

})()

However, in node.js you don't need to do this. Instead you can simply call the function when you require the module. So in node.js, this does the exact same thing in terms of creating a private scope:

module.exports = function () {

}

So tell your friend you're already wrapping the entire script in a function.

This is perhaps the first case that I see the harm in naming things and design patterns. In this case your friend is thinking IIFE. But IIFE is nothing special. IIFE doesn't create a private scope. It's functions that create scope. IIFE is just a means for the function to call itself. Which is why I prefer calling it self-calling-function to avoid giving it a sense of magic which may cause some people to perceive it as something special.

Upvotes: 1

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123473

No, IIFEs aren't required with Node.js.

They can be useful for any scripts that may be used in multiple environments (UMD).

But, each module/file executed by Node.js is given a "module scope," similar to the scope an IIFE provides, as described under "Globals":

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.

Though, there is still a global scope with Node.js. When a module creates a global, it will be accessible in other modules used by the same process.

foo = 'bar'; // lack of `var` defines a global

console.log(global.foo); // 'bar'

Upvotes: 12

Related Questions