title: JavaScript closure and pointers situation
body: <p>I have been doing some practice example with closures, and I wanted to dive into exactly what is happening in the following situation:</p>
  
  <pre><code>function createClosure(v, obj){
      var numb = v;
      var object = obj;
      return function(){console.log("v = " + numb + "\nobj = " + object.value)}
  }
  
  var v = 2;
  var obj = {value: 4};
  var function1  = createClosure(v, obj);
  </code></pre>
  
  <p><strong>Situation 1:</strong></p>
  
  <p>When <code>function1();</code> is run, I get the following result in the console:</p>
  
  <pre><code>v = 2
  obj = 4
  </code></pre>
  
  <p>So far so good. BUT now I tried to modify the variables outside of the closure, and then run <code>function1</code> again:</p>
  
  <p><strong>Situation 2:</strong></p>
  
  <pre><code>v = 6;
  obj.value = 11;
  function1();
  </code></pre>
  
  <p>and I got an interesting result:</p>
  
  <pre><code>v = 2
  obj = 11
  </code></pre>
  
  <p>So when modifying v, that doesn't affect the value of <code>numb</code> inside the closure, but when modifying <code>obj.value</code> it does affect the value of object.value.</p>
  
  <p><strong>QUESTION:</strong> Why is this the case? Why the "double" standard so to speak. I initially would have thought that <code>numb</code> would have obtained a copy of <code>v</code>, as well as <code>object</code> obtaining a copy of <code>obj</code>, and hence not being linked with each other, but from the results, that seems not to be the case.</p>
  
  <p>I might guess that it has to do with the way pointers work in JavaScript and it varying for how they interact with variables / objects in closure situations, but that is a guess.</p>