如何确定两个ES6类对象实例之间的相等性?例如:
最后两个语句返回值为false,但我希望它们返回true,以比较实例属性而不是对象引用。
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
(new Rectangle(1, 1)) === (new Rectangle(1, 1))
(new Rectangle(3, 0)) === (new Rectangle(9, 3))
最后两个语句返回值为false,但我希望它们返回true,以比较实例属性而不是对象引用。
===
不会让你得到结果,遗憾的是。 - Dellirium.equals()
方法。 - Sebastian Speitel