如何确定两个 JavaScript 对象的相等性? [英] How to determine equality for two JavaScript objects?

查看:43
本文介绍了如何确定两个 JavaScript 对象的相等性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

严格相等运算符会告诉您两个对象类型是否相等.但是,有没有办法判断两个对象是否相等,很像 Java 中的哈希码值?

A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java?

Stack Overflow 问题 JavaScript 中是否有任何类型的 hashCode 函数? 与此问题类似,但需要更学术的答案.上面的场景说明了为什么有必要拥有一个,我想知道是否有任何等效的解决方案.

Stack Overflow question Is there any kind of hashCode function in JavaScript? is similar to this question, but requires a more academic answer. The scenario above demonstrates why it would be necessary to have one, and I'm wondering if there is any equivalent solution.

推荐答案

简答

简单的答案是:不,没有通用的方法来确定一个对象在您的意义上与另一个对象相等.例外情况是您严格认为对象是无类型的.

The simple answer is: No, there is no generic means to determine that an object is equal to another in the sense you mean. The exception is when you are strictly thinking of an object being typeless.

长答案

该概念是一个 Equals 方法的概念,该方法比较对象的两个不同实例以指示它们在值级别是否相等.但是,定义 Equals 方法应该如何实现取决于特定类型.对具有原始值的属性进行迭代比较可能还不够:一个对象可能包含与相等性无关的属性.例如,

The concept is that of an Equals method that compares two different instances of an object to indicate whether they are equal at a value level. However, it is up to the specific type to define how an Equals method should be implemented. An iterative comparison of attributes that have primitive values may not be enough: an object may contain attributes which are not relevant to equality. For example,

 function MyClass(a, b)
 {
     var c;
     this.getCLazy = function() {
         if (c === undefined) c = a * b // imagine * is really expensive
         return c;
     }
  }

在上面的例子中,c对于判断 MyClass 的任意两个实例是否相等并不重要,只有 ab 是重要的.在某些情况下,c 可能因实例而异,但在比较中并不重要.

In this above case, c is not really important to determine whether any two instances of MyClass are equal, only a and b are important. In some cases c might vary between instances and yet not be significant during comparison.

请注意,当成员本身也可能是类型的实例时,此问题适用,并且每个成员都需要具有确定相等性的方法.

Note this issue applies when members may themselves also be instances of a type and these each would all be required to have a means of determining equality.

更复杂的是,在 JavaScript 中,数据和方法之间的区别是模糊的.

Further complicating things is that in JavaScript the distinction between data and method is blurred.

一个对象可能引用一个作为事件处理程序调用的方法,这可能不会被视为其值状态"的一部分.而另一个对象很可能会被分配一个执行重要计算的函数,从而使这个实例与其他实例不同,仅仅是因为它引用了不同的函数.

An object may reference a method that is to be called as an event handler, and this would likely not be considered part of its 'value state'. Whereas another object may well be assigned a function that performs an important calculation and thereby makes this instance different from others simply because it references a different function.

一个对象的现有原型方法之一被另一个函数覆盖了怎么办?它是否仍然可以被视为等同于另一个相同的实例?这个问题只能在每种类型的每种特定情况下得到回答.

What about an object that has one of its existing prototype methods overridden by another function? Could it still be considered equal to another instance that it otherwise identical? That question can only be answered in each specific case for each type.

如前所述,异常将是一个严格无类型的对象.在这种情况下,唯一明智的选择是对每个成员进行迭代和递归比较.即便如此,人们也不得不问一个函数的价值"是什么?

As stated earlier, the exception would be a strictly typeless object. In which case the only sensible choice is an iterative and recursive comparison of each member. Even then one has to ask what is the 'value' of a function?

这篇关于如何确定两个 JavaScript 对象的相等性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆