title: Get stuck in realizing the Closure feature (javascript)
body: <p>As a javascript beginner, I thought I realized the Closure feature until seeing the trap from the <a href="https://stackoverflow.com/a/111111/1613961">sample code</a> below. I tried to modify the code to pop up the result which many C/C++/C#/Java programmers expect as,</p>
  
  <p>"item1 1"</p>
  
  <p>"item2 2"</p>
  
  <p>"item3 3"</p>
  
  <p>After one hour struggling, I still can't do that. Could any javascript master teach me how to modify it ? Thank you.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function buildList(list) {
      var result = [];
      for (var i = 0; i &lt; list.length; i++) {
          var item = 'item' + list[i];
          result.push( function() {alert(item + ' ' + list[i])} );
      }
      return result;
  }
  
  function testList() {
      var fnlist = buildList([1,2,3]);
      // Using j only to help prevent confusion -- could use i.
      for (var j = 0; j &lt; fnlist.length; j++) {
          fnlist[j]();
      }
  }
  
  testList();</code></pre>
  </div>
  </div>
  </p>