I have the following example code:
class NumberWrapper {
data;
constructor(data) {
this.data = data;
}
}
let x1 = new NumberWrapper(1);
let x2 = new NumberWrapper(1);
let map = new Map();
map.set(x1, "X1");
console.log(map.get(x1)); // "X1"
console.log(map.get(x2)); // undefined
If x1
and x2
have the same data, why is the hash different for the data structures? Is the hash based on the memory address?
If so, how can I get around this and possibly implement my hash function that the map uses instead?