I need to do some experiment, and I need to use some kind of unique identifier for objects in JavaScript so I can see if they are the same.
I don’t want to use equality operators. Instead, I need something like the id() function in Python.
Does something like this exist?
Update My original answer below was written 6 years ago in a style befitting the times and my understanding. In response to some conversation in the comments, a more modern approach to this is as follows:
If you have archaic browser requirements, check here for browser compatibility for
Object.defineProperty.The original answer is kept below (instead of just in the change history) because I think the comparison is valuable.
You can give the following a spin. This also gives you the option to explicitly set an object’s ID in its constructor or elsewhere.
Take care to make sure that whatever member you use to internally store the unique ID doesn’t collide with another automatically created member name.