title: node.js keep the local variable in function, why?
body: <p>I have a question when I do the node.js async coding.
  Here is the example codes:</p>
  
  <pre><code>function foo(arg) {
    var a = arg;
    console.log(a + ' start');
    setTimeout(function () {console.log(a);}, 500);
  };
  foo(1);
  foo(2);
  </code></pre>
  
  <p>It outputs:</p>
  
  <pre><code>1 start
  2 start
  1
  2
  </code></pre>
  
  <p>I'm confused. I thought it should output↓, because the local variable is changed by the foo(2)</p>
  
  <pre><code>1 start
  2 start
  2
  2
  </code></pre>
  
  <p>Could you guys please tell me why/how node.js keep the local variable for the internal callback function access?
  Thanks a lot!</p>