title: JavaScript: is it possible for closures to access variables in the enclosing scope by value rather than by reference like in PHP?
body: <p>The following JavaScript code demonstrates how
  JavaScript (closure) functions access the variables
  in their enclosing environment by reference rather
  than by value.</p>
  
  <pre><code>var sum; // global variable
  
  function outer() {
    var hundred_more = 100;
    sum = function (a, b) {
      return a + b + (++hundred_more);
    };
    sum2 = function (a, b) {
      return a + b + (++hundred_more);
    };
    alert(hundred_more);
  }
  outer(); // define sum at the global scope and print 100
  alert(sum(5, 10)); // 116
  alert(sum(5, 10)); // 117
  alert(sum2(5, 10)); // 118
  alert(sum2(5, 10)); // 119
  outer(); // redefine sum at the global scope and print 100
  alert(sum(5, 10)); // 116
  alert(sum(5, 10)); // 117
  alert(sum2(5, 10)); // 118
  alert(sum2(5, 10)); // 119
  </code></pre>
  
  <p>The same code in PHP looks as follows:</p>
  
  <pre><code>&lt;?php
  
  $sum = null; // global variable
  $sum2 = null; // global variable
  
  function outer() {
    global $sum;
    global $sum2;
    $hundred_more = 100;
    $sum = function ($a, $b) use (&amp;$hundred_more) {
      return $a + $b + (++$hundred_more);
    };
    $sum2 = function ($a, $b) use (&amp;$hundred_more) {
      return $a + $b + (++$hundred_more);
    };
    var_dump($hundred_more);
  }
  outer(); // define sum at the global scope and print 100
  var_dump($sum(5, 10)); // 116
  var_dump($sum(5, 10)); // 117
  var_dump($sum2(5, 10)); // 118
  var_dump($sum2(5, 10)); // 119
  outer(); // redefine sum at the global scope and print 100
  var_dump($sum(5, 10)); // 116
  var_dump($sum(5, 10)); // 117
  var_dump($sum2(5, 10)); // 118
  var_dump($sum2(5, 10)); // 119
  </code></pre>
  
  <p>To pass the variable from the enclosing environment by value rather than by reference
  in PHP you simply omit the ampersand (&amp;) in front of the variables after the use keyword.
  This causes the value to be copied into the function's closure scope at the time the
  function is defined, so that all 'var_dumps' print the value 116. Is it possible to
  somehow achieve this same construct in JavaScript (with variables passed to the
  inner closure by value rather than by reference)?</p>
  
  <p>Thanks.</p>