title: Scope and maintainance of variable state in self invoking anonymous function vs anonymous functions
body: <p>I want to understand the way  anonymous self invoking function is able to maintain the last updated state of variable but why is it not possible in case of normal anonymous functions .Both functions are closures
   still are behaving differently while maintaining the previous state of variable.
  ex:</p>
  
  <pre><code>var selfInvokingfunc= (function () {
      var a=2;
      var myprivatefunction = function () {
          a=a+2;
          console.log(a);
  
      }
      return {
          mypublicfunction : function () {
              myprivatefunction();
          }
      }
  })();
  
  selfInvokingfunc.mypublicfunction() ;//a is 4
  selfInvokingfunc.mypublicfunction();  // a is 6
  
  var nonSelfInvokingAnonymousFunction = function () {
      var a=2;
      var myprivatefunction = function () {
          a=a+2;
          console.log(a);
      }
  
      return {
          mypublicfunction : function () {
              myprivatefunction();
          }
      }
  };
  
  nonSelfInvokingAnonymousFunction().mypublicfunction(); //a is 2
  nonSelfInvokingAnonymousFunction().mypublicfunction(); //a is 2
  </code></pre>
  
  <p>Same works fine for  non self invoking if invoked as :</p>
  
  <pre><code>var temp=nonSelfInvokingAnonymousFunction();
  temp.mypublicfunction() ; // a is 4
  temp.mypublicfunction(); // a is 6
  </code></pre>
  
  <p>Please help me understand the same.</p>