title: How can I maintain consistency in a 2-way object relationship in JavaScript?
body: <p>If I have a 2-way relationship between 2 objects, such that <strong>A</strong> relates to <strong>B</strong> and <strong>B</strong> relates to <strong>A</strong>, how can I keep this consistent such that the 2 objects will always refer back to each other?</p>
  
  <p>I'm struggling to put my very simple problem into words, so here's a very simple example. I start with <code>Husband</code> and <code>Wife</code>:</p>
  
  <pre><code>function Husband() { this.wife; }
  function Wife() { this.husband; }
  
  var harry = new Husband();
  var wendy = new Wife();
  
  harry.wife = wendy;
  wendy.husband = harry;
  </code></pre>
  
  <p>Logically, if Harry's wife is Wendy, then Wendy's husband is Harry.</p>
  
  <p>I need a way of keeping this relationship consistent. So I create a setter method on <code>Husband</code> and denote that the <code>wife</code> variable should be treated as private by prefixing with an underscore.</p>
  
  <pre><code>function Husband() {
      this._wife;
      this.setWife = function(wife) {
          this._wife = wife;
          wife.husband = this;
      }
  }
  </code></pre>
  
  <p>Describing this relationship is now simple and encourages consistency:</p>
  
  <pre><code>harry.setWife(wendy);
  </code></pre>
  
  <p>Likewise, it would be nice to have the reverse option:</p>
  
  <pre><code>wendy.setHusband(harry);
  </code></pre>
  
  <p>To do this I create a <code>setHusband</code> method on <code>Wife</code>, and tweak <code>Husband</code> as needed.</p>
  
  <pre><code>function Husband() {
      this._wife;
      this.setWife = function(wife) {
          this._wife = wife;
          // wife.husband = this; // &lt;-- husband is now _husband (private)...
          wife.setHusband(this);  // &lt;-- switching to a public method
      }
  }
  
  function Wife() {
      this._husband;
      this.setHusband = function(husband) {
          this._husband = husband;
          husband._wife = this;  // &lt;-- Oops! _wife is private!
          husband.setWife(this); // &lt;-- Oops! this results in an infinite loop!
      }
  }
  </code></pre>
  
  <p>At this point I run into a snag. My new <code>setHusband</code> method needs to be able to maintain consistency, but <code>wife</code> is now <code>_wife</code> (private), and calling <code>setWife</code> causes an infinite loop because they reciprocate each other.</p>
  
  <p>I could create another set of methods like <code>reallyJustSetHusband</code> but that seems silly.</p>
  
  <p>My conundrum isn't particular to JavaScript, but I've mentioned it in the question in case there's a particular approach needed.</p>
  
  <p>What is the best way to achieve consistency between these 2 objects? Is there something I've overlooked?</p>
  
  <h1>Similar Pattern in the DOM</h1>
  
  <p>In the DOM, if you call <code>parent.appendChild(child)</code>, then <code>child.parentNode === parent</code>. They are never inconsistent. If a parent has a child the child has the same parent. Other relationships such as <code>nextSibling</code> are also kept consistent.</p>