为什么需要深度对象比较?

Why do you need deep object comparison?

参考:https://dmitripavlutin.com/how-to-compare-objects-in-javascript/

参照参考,深度对比为:

The deep equality is similar to the shallow equality, but with one difference. During the shallow check, if the compared properties are objects, a recursive shallow equality check is performed on these nested objects.

const objA = {
  propA: 123
  propB: 'a'
  propObj1: { // assume reference: 1234
     propObj1: 'abc'
  }
}

const objB = {
  propA: 123
  propB: 'a'
  propObj1: { // assume reference: 1234
     propObj1: 'abc'
  }
}

有没有可能objAobjB浅比较相等,深度比较不相等?

因为他们的propObj1有相同的引用,改变为objA.propObj1也会反映到objB.propObj1,这意味着他们在深度比较中也是相等的。你能举个例子,浅比较是true,深度比较是false吗?

您回答了自己的问题!你不得不在你的例子中说“假设参考:1234”这一事实表明你知道如果 propObj1 指向 不同但等于 值,浅比较将不起作用(即你的 exact 示例,但没有“假设参考:1234”评论。

下面的

objAobjB 将无法通过浅层相等检查,但会通过深层相等检查:

const objA = {
  propA: 123
  propB: 'a'
  propObj1: {
     propObj1: 'abc'
  }
}

const objB = {
  propA: 123
  propB: 'a'
  propObj1: {
     propObj1: 'abc'
  }
}