Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 647
Sikorski Avatar asked Sep 28 '14 09:09

Sikorski


People also ask

How do you wrap a JavaScript function call?

wrap() is used to wrap a function inside other function. It means that the first calling function (a function which is calling another function in its body) is called and then the called function is being executed. If the calling function does not call the called function then the second function will not be executed.

What is the reason of wrapping entire js file into function block?

The purpose of wrapping is to a namespace and control the visibility of member functions. It wraps the code inside a function scope and decreases clashing with other libraries. This is what we call Immediately Invoked Function Expression (IIFE) or Self Executing Anonymous Function.

What surrounds the code inside a function?

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions. The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries.

What is a wrapper function JS?

In programming languages such as JavaScript, a wrapper is a function that is intended to call one or more other functions, sometimes purely for convenience, and sometimes adapting them to do a slightly different task in the process. For example, SDK Libraries for AWS are examples of wrappers.


2 Answers

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'
like image 123
Jonathan Lonowski Avatar answered Sep 23 '22 21:09

Jonathan Lonowski


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.

like image 27
slebetman Avatar answered Sep 26 '22 21:09

slebetman