You just have to save the reference to the object:
var myMap = new Map();
var myKey = new Tuple(1,1);
myMap.set( myKey, "foo");
myMap.set('bar', "foo");
myMap.has(myKey); // returns true; myKey === myKey
myMap.has(new Tuple(1,1)); // returns false; new Tuple(1,1) !== myKey
myMap.has('bar'); // returns true; 'bar' === 'bar'
Edit: Here is how to use an object to achieve what you want, which is to compare objects by their values rather than by reference:
function Tuple (x, y) {
this.x = x;
this.y = y;
}
Tuple.prototype.toString = function () {
return 'Tuple [' + this.x + ',' + this.y + ']';
};
var myObject = {};
myObject[new Tuple(1, 1)] = 'foo';
myObject[new Tuple(1, 2)] = 'bar';
console.log(myObject[new Tuple(1, 1)]); // 'foo'
console.log(myObject[new Tuple(1, 2)]); // 'bar'
These operations will run in constant time on average, which is much faster than searching through a Map for a similar object key in linear time.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…