Q:

How can I use a object reference stored in a variable as a string?

If I have a variable foo that hold a reference to an object:
var foo = someObj;

How can I then use the name of the object as a string?
I tried:
var bar = foo.valueOf()

But that just returned another reference to the object.
What I have is an algorithm that selects from a large number of objects.  I then want to use the name of that object to select from amongst a group of HTML elements.  Using the following does not work either (returns null):
document.getElementById(foo)

Thank you.

A:

There is no reliable way to get the name of the object.
Example:
var obj = {};
var obj1 = obj;
var obj2 = obj;

magically_get_and_print_name(obj);  // What to print? "obj"? "obj1"? "obj2"?

Methods to get the name in some cases:

Function declarations - funcreference.name (non-standard, though well-supported)
Constructor instances - instance.constructor.name

