I want to produce a hash derived from an Object
's reference, and then associate the hash string to the object itself into a dictionary.
I have heard of object-hash and attempted the following implementation:
const objectHash = require('object-hash');
class A {
a: string;
constructor() {
this.a = '1';
}
}
let a = new A();
let ab = new A();
console.log(objectHash(a)); // 712041d72943c4794bb23d1d455e17b3a4ea17f5
console.log(objectHash(ab));// 712041d72943c4794bb23d1d455e17b3a4ea17f5
In despite of a
and ab
being different objects (although instances of the same class), the hash produced is equal, which is not the expected result. That is because the library hashes object values rather than object reference.
Expected result
let foo = new A();
let bar = new A();
const fooH = objectHash(foo); <-- Produces a unique hash string representing the reference of "foo"
const barH = objectHash(bar); <-- Produces a unique hash string representing the reference of "bar"
let objects = {
fooH: foo,
barH: bar
}
This could probably be achieved in C++ or C# by using pointers and addresses, but what about Javascript? If anybody can guide me on the right path to incapsulate an object's reference into a string, it would be appreciated.