10
Several languages have a means of obtaining the "identity" of an object, i.e. an [integer] number that is unique to each object, such that different objects have different identities. Examples:
- Java (
System.identityHashCode
)
As far as reasonably practical, the hashcode method defined by the Object class returns distinct integers for distinct objects. (This is typically implemented by converting the object’s internal address into an integer, but this implementation technique is not required by the Java programming language.)
- Python (
id
)
Returns the "identity" of an object. This is an integer (or long integer) that is guaranteed to be unique and constant to that object during its life cycle. Two objects with non-overlapping lifetimes can have the same value for id().
Is there similar functionality in Javascript? Preferably standardized, that worked in all browsers and other environments (Rhino, V8). Searching for "Identity" I only find references to the operator (===
)...
I know that some environments do not support this concept (.NET apparently does not do so for reasons of optimization), but in general this is not a problem because they are more complete languages, which have the concepts of destructor, finisher and/or weak references. As far as I know, Javascript does not support this, so a way to get a numeric value that only represents an object would be very useful (a simple reference to the object does not serve as it would prevent it from being collected as garbage).
Can you illustrate in what scenario this id would be used? Why would you save the id of an object that has already been collected as garbage?
– rodrigorgs
@Rodrigorgs Just to know when and if it was collected as garbage. As Javascript [yet] does not support weak references, and there doesn’t seem to be any workaround satisfactory, my only way out is to try to "simulate" them through an indirect reference (I’m still working on the details). For cases of use for weak references, has an interesting material here (Notice: PDF).
– mgibsonbr
The language does not foresee any way to get this type of identity, so if any interpreter includes this, it is in a non-standard way.
– bfavaretto