title: Use of anonymous functions in nodejs
body: <p>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?</p>
  
  <p>The reason I ask this is because popular node modules (like async.js) extensively use anonymous functions. </p>
  
  <p><strong>Example with anonymous function</strong></p>
  
  <p><strong><em>1) test_module.js</em></strong></p>
  
  <pre><code>(function(){
  
  var test_module = {}; 
  var a = "Hello";
  var b = "World";
  
  test_module.hello_world = function(){
  
      console.log(a + " " + b);
  
  };
  
  module.exports = test_module;
  
  
  }());
  </code></pre>
  
  <p><strong><em>2) test.js</em></strong></p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p><strong><em>Output:</em></strong> </p>
  
  <pre><code>Hello World
  [ReferenceError: a is not defined]
  [ReferenceError: b is not defined]
  </code></pre>
  
  <p><strong>Example without anonymous function</strong></p>
  
  <p><strong><em>1) test_module2.js</em></strong></p>
  
  <pre><code>var test_module = {}; 
  var a = "Hello";
  var b = "World";
  
  test_module.hello_world = function(){
  
      console.log(a + " " + b);
  
  };
  
  module.exports = test_module;
  </code></pre>
  
  <p><strong><em>2) test2.js</em></strong></p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p><strong><em>Output:</em></strong> </p>
  
  <pre><code>Hello World
  [ReferenceError: a is not defined]
  [ReferenceError: b is not defined]
  </code></pre>