I need to store a list of points and check if a new point is already included in that list
class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
window.onload = () => {
var points : Point[] = [];
points.push(new Point(1,1));
var point = new Point(1,1);
alert(points.indexOf(point)); // -1
}
Obviously typescript uses comparison by reference but in this case that doesn't make sense. In Java or C# I would overload the equals
method, in typescript that doesn't seem to be possible.
I considered to loop through the array with foreach
and check each entry for equality, but that seems rather complicated and would bloat the code.
Is there something like equals
in typescript ? How can I implement my own comparisons ?