我需要存储点列表并检查该列表中是否已包含新点
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
}
Run Code Online (Sandbox Code Playgroud)
显然打字稿使用引用比较,但在这种情况下没有意义。在 Java 或 C# 中,我会equals在似乎不可能的打字稿中重载该方法。
我考虑过遍历数组foreach并检查每个条目是否相等,但这看起来相当复杂并且会使代码膨胀。
equals打字稿中有类似的东西吗?我怎样才能实现我自己的比较?
originaltraits:{
artistic:25,
athletic:24,
goodLooks:70,
happiness:0,
health:81
}
newtraits:{
artistic:25,
athletic:24,
goodLooks:70,
happiness:0,
health:81
}
Run Code Online (Sandbox Code Playgroud)
我有这两个对象我想比较这些对象的字段而不是在Angular中使用对象引用然后如何比较?