title: How to get javascript object references or reference count?
body: <h2>How to get reference count for an object</h2>
  
  <ul>
  <li>Is it possible to determine if a javascript object has <strong>multiple references</strong> to it? </li>
  <li>Or if it has references <strong>besides the one I'm accessing it with</strong>? </li>
  <li>Or even just to get the <strong>reference count</strong> itself?</li>
  <li>Can I find this information from javascript itself, or will I need to keep track of my own reference counters.</li>
  </ul>
  
  <p>Obviously, there must be at least one reference to it for my code access the object. But what I want to know is if there are any other references to it, or if my code is the only place it is accessed. I'd like to be able to delete the object if nothing else is referencing it.</p>
  
  <p><strong>If you know the answer, there is no need to read the rest of this question.</strong> Below is just an example to make things more clear.</p>
  
  <hr>
  
  <h2>Use Case</h2>
  
  <p>In my application, I have a <code>Repository</code> object instance called <code>contacts</code> that contains an array of <strong>ALL</strong> my contacts. There are also multiple <code>Collection</code> object instances, such as  <code>friends</code> collection and a <code>coworkers</code> collection. Each collection contains an array with a different set of items from the <code>contacts</code> <code>Repository</code>.</p>
  
  <h2>Sample Code</h2>
  
  <p>To make this concept more concrete, consider the code below. Each instance of the <code>Repository</code> object contains a list of all items of a particular type. You might have a repository of <strong>Contacts</strong> and a separate repository of <strong>Events</strong>. To keep it simple, you can just get, add, and remove items, and add many via the constructor.</p>
  
  <pre><code>var Repository = function(items) {
    this.items = items || [];
  }
  Repository.prototype.get = function(id) {
    for (var i=0,len=this.items.length; i&lt;len; i++) {
      if (items[i].id === id) {
        return this.items[i];
      }
    }
  }
  Repository.prototype.add = function(item) {
    if (toString.call(item) === "[object Array]") {
      this.items.concat(item);
    }
    else {
      this.items.push(item);
    }
  }
  Repository.prototype.remove = function(id) {
    for (var i=0,len=this.items.length; i&lt;len; i++) {
      if (items[i].id === id) {
        this.removeIndex(i);
      }
    }
  }
  Repository.prototype.removeIndex = function(index) {
    if (items[index]) {
      if (/* items[i] has more than 1 reference to it */) {
        // Only remove item from repository if nothing else references it
        this.items.splice(index,1);
        return;
      }
    }
  }  
  </code></pre>
  
  <p>Note the line in <code>remove</code> with the comment. I only want to remove the item from my master repository of objects if no other objects have a reference to the item. Here's <code>Collection</code>:</p>
  
  <pre><code>var Collection = function(repo,items) {
    this.repo = repo;
    this.items = items || [];
  }
  Collection.prototype.remove = function(id) {
    for (var i=0,len=this.items.length; i&lt;len; i++) {
      if (items[i].id === id) {
        // Remove object from this collection
        this.items.splice(i,1);
        // Tell repo to remove it (only if no other references to it)
        repo.removeIndxe(i);
        return;
      }
    }
  }
  </code></pre>
  
  <p>And then this code uses <code>Repository</code> and <code>Collection</code>:</p>
  
  <pre><code>var contactRepo = new Repository([
      {id: 1, name: "Joe"},
      {id: 2, name: "Jane"},
      {id: 3, name: "Tom"},
      {id: 4, name: "Jack"},
      {id: 5, name: "Sue"}
    ]);
  
  var friends = new Collection(
    contactRepo,
    [
      contactRepo.get(2),
      contactRepo.get(4)
    ]
  );
  
  var coworkers = new Collection(
    contactRepo,
    [
      contactRepo.get(1),
      contactRepo.get(2),
      contactRepo.get(5)
    ]
  );
  
  contactRepo.items; // contains item ids 1, 2, 3, 4, 5 
  friends.items;  // contains item ids 2, 4
  coworkers.items;  // contains item ids 1, 2, 5
  
  coworkers.remove(2);
  
  contactRepo.items; // contains item ids 1, 2, 3, 4, 5 
  friends.items;  // contains item ids 2, 4
  coworkers.items;  // contains item ids 1, 5
  
  friends.remove(4);
  
  contactRepo.items; // contains item ids 1, 2, 3, 5 
  friends.items;  // contains item ids 2
  coworkers.items;  // contains item ids 1, 5
  </code></pre>
  
  <p>Notice how <code>coworkers.remove(2)</code> didn't remove id 2 from contactRepo? This is because it was still referenced from <code>friends.items</code>. However, <code>friends.remove(4)</code> causes id 4 to be removed from <code>contactRepo</code>, because no other collection is referring to it.</p>
  
  <h2>Summary</h2>
  
  <p>The above is what I want to do. I'm sure there are ways I can do this by keeping track of my own reference counters and such. But if there is a way to do it using javascript's built-in reference management, I'd like to hear about how to use it.</p>