title: Basic Javascript: Functions that Return Functions
body: <p>This is a very basic javascript question:</p>
  
  <p>after defining a function:</p>
  
  <pre><code>function a(){
      alert("A!");
      return function(){
          alert("B!")
      };
  }
  </code></pre>
  
  <p>If i call the function:</p>
  
  <pre><code>a();
  </code></pre>
  
  <p>Only "A!" is alert;
  But if i recreate a new function:</p>
  
  <pre><code>var newFunc = a();
  newFunc();
  </code></pre>
  
  <p>Both "A!" and "B!" are alerted.</p>
  
  <p>Can someone explain why?
  Millions THX!!</p>
  
  <p><strong>This is extracted from p78, Object-Oriented JavaScript, Stoyan Stefanov, PACKT PUBLISHING</strong></p>