How to get a reference counter for an object
- Is it possible to determine if a javascript object has multiple links ?
- Or, if he has links other than the one to which I refer to him using ?
- Or even just get a reference count ?
- Can I find this information from javascript itself, or will I need to track my own link counts.
Obviously, for my code, there must be at least one link to access the object. But I want to know if there are any other links to it, or if my code is the only place it accesses. I would like to be able to delete an object if nothing refers to it.
If you know the answer, there is no need to read the rest of this question. The following is an example to make things clearer.
Use case
In my application, I have an instance of a Repository object called contacts that contains an array of ALL of my contacts. There are also several instances of Collection objects, such as the friends collection and the coworkers collection. Each collection contains an array with a different set of elements from the contacts Repository .
Code example
To make this concept more specific, consider the code below. Each instance of the Repository object contains a list of all elements of a particular type. You can have a Contacts repository and a separate Events repository. To make it simple, you can simply add, add and remove elements and add them through the constructor.
var Repository = function(items) { this.items = items || []; } Repository.prototype.get = function(id) { for (var i=0,len=this.items.length; i<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<len; i++) { if (items[i].id === id) { this.removeIndex(i); } } } Repository.prototype.removeIndex = function(index) { if (items[index]) { if () {
Notice the line in remove with the comment. I want to remove an object from my main object store if no other objects reference the element. Here Collection :
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<len; i++) { if (items[i].id === id) {
And then this code uses Repository and Collection :
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;
Note that coworkers.remove(2) not removed id 2 from contactRepo? This is because it still refers to friends.items . However, friends.remove(4) causes id 4 to be removed from contactRepo , because no other collection references it.
Summary
Above was what I want to do. I am sure there are ways to do this by tracking my own link counts, etc. But if there is a way to do this using the built-in javascript link management, I would like to know how to use it.