3

Is there a use for anonymous functions when writing a nodejs module. I understand that we use anonymous function to limit the scope of the variables/functions used to a specific module. However, in nodejs, we use modules.exports to made a function or variable available outside the module, hence shouldn't an anonymous function be unnecessary?

The reason I ask this is because popular node modules (like async.js) extensively use anonymous functions.

Example with anonymous function

1) test_module.js

(function(){

var test_module = {}; 
var a = "Hello";
var b = "World";

test_module.hello_world = function(){

    console.log(a + " " + b);

};

module.exports = test_module;


}());

2) test.js

var test_module = require("./test_module");

test_module.hello_world();


try {   
    console.log("var a is " + a + "in this scope");
}
catch (err){

    console.log(err);
}

try {   
    console.log("var a is " + b + "in this scope");
}
catch (err){

    console.log(err);
}

Output:

Hello World
[ReferenceError: a is not defined]
[ReferenceError: b is not defined]

Example without anonymous function

1) test_module2.js

var test_module = {}; 
var a = "Hello";
var b = "World";

test_module.hello_world = function(){

    console.log(a + " " + b);

};

module.exports = test_module;

2) test2.js

var test_module = require("./test_module2");

test_module.hello_world();


try {   
    console.log("var a is " + a + "in this scope");
}
catch (err){

    console.log(err);
}

try {   
    console.log("var a is " + b + "in this scope");
}
catch (err){

    console.log(err);
}

Output:

Hello World
[ReferenceError: a is not defined]
[ReferenceError: b is not defined]
1
  • 4
    Node.js modules provide encapsulation them-selves. So there is no need to wrap assignment of module.exports this way. You might need it if you have nested classes. But that fact itself would actually hint to wrap this nested class into another module... Commented Apr 7, 2015 at 13:11

1 Answer 1

9

You absolutely DO NOT need the anonymous function

Node guarantees you a clean "namespace" to work in with each file

The only things "visible" from each file/module are things that you explicitly export using module.exports


Your test_module2.js is much preferred though I would still make a couple changes. Most notably, you don't have to define myModule as an object in your file. You can think of the file as a module already.

test_module3.js

var a = "Hello";
var b = "World";

function helloWorld() {
  console.log(a, b);
}

exports.helloWorld = helloWorld;
Sign up to request clarification or add additional context in comments.

6 Comments

And any objects assigned as properties of the global object.
@MS_SL I am politely asking you to delete your comment. Do not mention global in node.js to me. I don't want even a hint of a recommendation of its usage in the context of my answers. Honestly, I'd rather people don't even know it exists in node.js.
Thank you for your answer. I was curious because I was using a popular node module that uses anonymous functions. Now I understand that the reason for this is because the module can be used in both node and front end JS (included using a script tag).
@naomik No matter how you personally feel about it, hiding information is not in the spirit of StackOverflow. Explain why people should not use it instead. Information trumps ignorance every time.
@SivaDotRender that is correct. Node.js is used to build a lot of front-end modules. You'll see a variety of closures for modules that end up being loaded in such a way. One resource you might find useful is umdjs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.