title: Javascript object child referencing
body: <p>I was just doing a small test to see how Javascript would respond to changing a child object's value within a parent object.  I wanted to see if the parent referenced the child and would keep up to date with the new values, or if it would only keep the initial state of the child that it was instantiated with.</p>
  
  <p>I have the little module <strong>coords.js</strong></p>
  
  <pre><code>var NS = NS || {};
  
  NS.Coords = function(){
  
      var __parentArray = {};
      var __childArray = {};
  
      addToParent = function(){
          __childArray['value'] = "Initial";
          __parentArray['child'] = __childArray;
      },
  
      showParent = function(){
          console.log("Parent: ",__parentArray);
          console.log("Child within parent: ",__parentArray['child']);
      },
  
      changeChild = function(){
          __childArray['value'] = "Changed";
      },
  
      showChild = function(){
          console.log("Child: ",__childArray]);
      };
  
      return {
          addToParent: addToParent,
          showParent: showParent,
          changeChild: changeChild,
          showChild: showChild
      };
  
  }();
  </code></pre>
  
  <p>And in <strong>main.js</strong></p>
  
  <pre><code>var NS = NS || {};
  
  // @codekit-prepend "Coords.js"
  
  console.log("=============================");
  console.log("Startpoint point");
  console.log("=============================");
  var coords = NS.Coords;
  coords.addToParent();
  coords.showChild();
  coords.showParent();
  
  console.log("=============================");
  console.log("Changed child value");
  console.log("=============================");
  coords.changeChild();
  coords.showChild();
  coords.showParent();
  </code></pre>
  
  <p>If you run this, you see in the console that when shown directly, the child shows the expected "Initial" and then "Changed" values.</p>
  
  <p>The parent, however, always shows the "Changed" value of the child object it references.  Even before changeChild() is called.  No idea why.  Without even changing the value it shows that it's been changed.  Am I missing something super simple, or am I misunderstanding what's going on here?</p>