Exploring the capabilities of ES2015 Maps has been quite exciting, as I'm starting to see its potential. However, I've encountered a use case that has me stumped on whether Maps can handle it.
Let's take a look at my class:
class A {
constructor(public a:number) {}
public equals(obj: A): boolean {
return this.a === obj.a;
}
}
From what I understand, ES2015 Maps utilize the === equality operator (with some exceptions). This raises the question - is it possible to customize the equality comparison and make Maps use the A
object's equals(...)
method if applicable?
My end goal is simple:
var map = new Map();
map.set(new A(8), 7)
map.get(new A(8)) // should return 7 instead of undefined
Do you believe that creating a custom Map class is necessary to achieve this behavior, where it checks if both objects are instances of A
and then calls the equals(...)
method?