title: How to copy the value of a reference in javascript?
body: <p>Some time ago I posted a question about <a href="https://stackoverflow.com/questions/1684917/what-questions-should-a-javascript-programmer-be-able-to-answer">what questions should a good javascript coder be able to answer</a>. Meder pointed out the following question: </p>
  
  <p>The following code makes clicks on any "a" element to alert(-1) due to the fact that "i" is hold in the onclick function as a reference and not as a value:</p>
  
  <pre><code>&lt;a href="#"&gt;text&lt;/a&gt;&lt;br&gt;&lt;a href="#"&gt;link&lt;/a&gt;
  &lt;script&gt;
  var as = document.getElementsByTagName('a');
  for ( var i = as.length; i--; ) {
      as[i].onclick = function() {
          alert(i);
          return false;
      }
  }
  &lt;/script&gt;
  </code></pre>
  
  <blockquote>
    <p>The question is: How to fix this
    implementation so that the onclick
    function holds the value of i and not
    it's reference?</p>
  </blockquote>
  
  <p>I don't know the answer. How to fix it? How to make i to be a copy of the reference value rather than the actual reference?</p>
  
  <p>Side questions: Are all variable types passed in as a reference? Or does it vary on whether it's a primitity type or an object?</p>
  
  <p>Any thoughts would be appreciated.</p>