Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason to define module.exports using an IIFE?

Tags:

My team doesn't have any experienced JS developers, but we are writing a library in Node and got a suggestion from a real JS developer that "We should make the js more modular - not to pollute the global namespace and to make it more readable to new-comers", and told us to do the following:

module.exports = (function(){
      return {
         nameToExpose: functionToExpose
         ...
    };
})();

rather than

module.exports.nameToExpose = functionToExpose;

What's the point of this, if any? The latter does not make any local declarations that would be scoped by the IIFE, and even if it did, they would be local to the module file and not global to the whole program that require()s it.

Some Googling and poking about this site does not turn up any answers on this particular question, though there are many other explanations of IIFEs that I have read (and which are summarized in the above comment). Some testing certainly reveals that the latter does not actually put functionToExpose in the global namespace, though its original name is recorded in the function type itself.

like image 949
Ryan Reich Avatar asked Sep 08 '15 17:09

Ryan Reich


People also ask

What is the purpose of module exports explain with example?

The main purpose of module. exports is to achieve modular programming. Modular programming refers to separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.

Why would you create a default export in a module?

Default Exports: Default exports are useful to export only a single object, function, variable. During the import, we can use any name to import.

How many exports can a module have?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

What keyword is used to bring module exports into scope?

require keyword refers to a function which is used to import all the variables and functions exported using the module. exports object from another module.


1 Answers

Pretty much no difference. The whole idea of Node.js, using require, having modules, etc., is specifically to separate concerns. I'd say (cautiously) that if you're doing it right, you shouldn't be needing to worry about "polluting" any sort of global scope. Anything within module.exports lives in that module.

When you're dealing with front-end stuff, that's when the global scope becomes something of a concern, because if a function or whatever isn't scoped (i.e., in an IIFE, or other function block), it has access to the global window object, and everything else has access to that function.

a real JS developer

Calling someone that is a red flag.

not to pollute the global namespace and to make it more readable to new-comers

If you're modularizing your code correctly, that shouldn't be a concern. There's a time and a place for IIFEs, but I see no reason why wrapping everything in an IIFE, which is already inside of a module, would somehow magically make the code "more modular" or any more readable to "new comers" than by simply using Node.js like it was designed:

module.exports = function() { ... } // whatever

and even if it did, they would be local to the module file and not global to the whole program that require()s it.

You are correct. I'd take whatever he's saying with a grain of salt. Maybe he knows of some specific use-cases where his approach has been helpful to him in the past, so I'd ask him specifically about that to see what he says. Other than that, I feel like you're on the right track.

like image 198
Josh Beam Avatar answered Sep 22 '22 17:09

Josh Beam