title: Closure example analysis
body: <p>so I would like to analyze a closure example, but i'm not sure if my reasoning is correct, so i'm wondering if someone could let me know if i'm right.</p>
  
  <pre><code>var gLogNumber, gIncreaseNumber, gSetNumber;
  function setupSomeGlobals() {
      // Local variable that ends up within closure
      var num = 42;
      // Store some references to functions as global variables
      gLogNumber = function() { console.log(num); }
      gIncreaseNumber = function() { num++; }
      gSetNumber = function(x) { num = x; }
  }
  
  setupSomeGlobals();
  gIncreaseNumber();
  gLogNumber(); // 43
  gSetNumber(5);
  gLogNumber(); // 5
  
  var oldLog = gLogNumber;
  
  setupSomeGlobals();
  gLogNumber(); // 42
  
  oldLog() // 5
  </code></pre>
  
  <p>Ok so in the line <code>setupSomeGlobals();</code> we call our function which contains three methods. </p>
  
  <p>Next we  call <code>gIncreaseNumber();</code>. Cool, so now <code>num - &gt; 43</code>, then we log it, to get 43 as expected! </p>
  
  <p>Now things are getting boring, so lets set this <code>num</code> to another value. Say 5 as per the line <code>gSetNumber(5);</code> Now <code>num = 5</code> , and of course we log it to get <code>5</code>.</p>
  
  <p>Now heres where things get <em>trippy</em> for me. So we say hey, lets give this variable <code>oldLog</code> a method with the same property as <code>glogNumber</code>. Alright,fair enough. </p>
  
  <p>So we call <code>setupSomeGlobals()</code>, and all the stuff we just did like increasing the number and setting the number previously gets overwritten since the code goes back to <code>var num = 42</code> ( Ok, this is what I'm not sure about)</p>
  
  <p>Now if we <code>gLobNumber()</code>, we get <code>42</code>, because with my reasoning everything gets reset to the beginning, where <code>var num = 42</code>. Nice!</p>
  
  <p>So now we call <code>oldLog()</code> which is pointing to <code>gLogNumber</code>. Great so this should be <code>42</code> since everything got reset when we did <code>setupSomeGlobals()</code>, but this outputs <code>5</code>. Erm <em>whats</em> going on?</p>