To illustrate the problem, consider the following simple object
function Key( val ) {
this._val = val;
}
Now I create an instance of ES6 Mapand load one entry into it, like this
var map = new Map(),
key1 = new Key( 'key' );
map.set( key1, 'some value' );
console.log( 'key1: ', map.has( key1 ) );
So far so good. However, the problem arises if I create an almost identical object key2like this
var key2 = new Key( 'key' );
So basically both keys are identical, but obviously key2not part of the map
console.log( 'key2: ', map.has( key2 ) );
// key2: false
JavaScript uses object references as the key here, so two separate objects will not point to the same value.
Now I would like to add something like a method hash()to the key prototypeso that both objects point to the same key. Is something like this possible?
, , factory Key . , , . , .