How to get javascript object references or reference count?

How to get reference count for an object

Is it possible to determine if a javascript object has multiple references to it?

Or if it has references besides the one I'm accessing it with?

Or even just to get the reference count itself?

Can I find this information from javascript itself, or will I need to keep track of my own reference counters.

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.

If you know the answer, there is no need to read the rest of this question. Below is just an example to make things more clear.

Use Case

In my application, I have a

Repository

object instance called

contacts

that contains an array of ALL my contacts. There are also multiple

Collection

object instances, such as

friends

collection and a

coworkers

collection. Each collection contains an array with a different set of items from the

contacts

Repository

.

Sample Code

To make this concept more concrete, consider the code below. Each instance of the

Repository

object contains a list of all items of a particular type. You might have a repository of Contacts and a separate repository of Events. To keep it simple, you can just get, add, and remove items, and add many via the constructor.

didn't remove id 2 from contactRepo? This is because it was still referenced from

friends.items

. However,

friends.remove(4)

causes id 4 to be removed from

contactRepo

, because no other collection is referring to it.

Summary

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.

In your example code it would seem simpler to forget the Repository, use a plain Array for your lists, and let standard garbage collection take care of dropping unused people. If you needed to get a list of all people in use, you'd just concat the friends and coworkers lists (and sort/uniquify them if you needed to).

Email codedump link for How to get javascript object references or reference count?