Basically, I’m trying to create an object of unique objects, a set. I had the brilliant idea of just using a JavaScript object with objects for the property names. Such as,
set[obj] = true;
This works, up to a point. It works great with string and numbers, but with other objects, they all seem to ‘hash’ to the same value and access the same property. Is there some kind of way I can generate a unique hash value for an object? How do strings and numbers do it, can I override the same behavior?
JavaScript objects can only use strings as keys (anything else is converted to a string).
You could, alternatively, maintain an array which indexes the objects in question, and use its index string as a reference to the object. Something like this:
Obviously it’s a little verbose, but you could write a couple of methods that handle it and get and set all willy nilly.
Edit:
This brings up another interesting point; you can define a toString method on the objects you want to hash, and that can form their hash identifier.