比较详情


¥Comparison details

  • 原始值与 == 操作符 进行比较,但 NaN 除外。如果双方都是 NaN,则视为相同。

    ¥Primitive values are compared with the == operator, with the exception of NaN. It is treated as being identical in case both sides are NaN.

  • 对象的 类型标签 应该是相同的。

    ¥Type tags of objects should be the same.

  • 仅考虑 可枚举的 "自有" 属性

    ¥Only enumerable "own" properties are considered.

  • 始终比较 <Error> 名称、消息、原因和错误,即使这些不是可枚举属性。

    ¥<Error> names, messages, causes, and errors are always compared, even if these are not enumerable properties.

  • 对象封装器 作为对象和展开的值进行比较。

    ¥Object wrappers are compared both as objects and unwrapped values.

  • Object 属性是无序比较的。

    ¥Object properties are compared unordered.

  • <Map> 键和 <Set> 项是无序比较的。

    ¥<Map> keys and <Set> items are compared unordered.

  • 当双方不同或任何一方遇到循环引用时,递归停止。

    ¥Recursion stops when both sides differ or either side encounters a circular reference.

  • 实现不测试对象的 [[Prototype]]

    ¥Implementation does not test the [[Prototype]] of objects.

  • 不比较 Symbol 属性。

    ¥Symbol properties are not compared.

  • WeakMap、WeakSet 和 <Promise> 实例在结构上不进行比较。只有当它们引用同一个对象时,它们才是相等的。任何不同的 WeakMapWeakSetPromise 实例之间的比较都会导致不相等,即使它们包含相同的内容。

    ¥WeakMap, WeakSet and <Promise> instances are not compared structurally. They are only equal if they reference the same object. Any comparison between different WeakMap, WeakSet, or Promise instances will result in inequality, even if they contain the same content.

  • <RegExp> 的 lastIndex、flags 和 source 总是被比较,即使它们不是可枚举的属性。

    ¥<RegExp> lastIndex, flags, and source are always compared, even if these are not enumerable properties.

以下示例不会抛出 AssertionError,因为基础类型是使用 == 操作符 进行比较的。

¥The following example does not throw an AssertionError because the primitives are compared using the == operator.

import assert from 'node:assert';
// WARNING: This does not throw an AssertionError!

assert.deepEqual('+00000000', false);const assert = require('node:assert');
// WARNING: This does not throw an AssertionError!

assert.deepEqual('+00000000', false);

"深度" 相等意味着子对象的可枚举 "自有" 属性也被评估:

¥"Deep" equality means that the enumerable "own" properties of child objects are evaluated also:

import assert from 'node:assert';

const obj1 = {
  a: {
    b: 1,
  },
};
const obj2 = {
  a: {
    b: 2,
  },
};
const obj3 = {
  a: {
    b: 1,
  },
};
const obj4 = { __proto__: obj1 };

assert.deepEqual(obj1, obj1);
// OK

// Values of b are different:
assert.deepEqual(obj1, obj2);
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }

assert.deepEqual(obj1, obj3);
// OK

// Prototypes are ignored:
assert.deepEqual(obj1, obj4);
// AssertionError: { a: { b: 1 } } deepEqual {}const assert = require('node:assert');

const obj1 = {
  a: {
    b: 1,
  },
};
const obj2 = {
  a: {
    b: 2,
  },
};
const obj3 = {
  a: {
    b: 1,
  },
};
const obj4 = { __proto__: obj1 };

assert.deepEqual(obj1, obj1);
// OK

// Values of b are different:
assert.deepEqual(obj1, obj2);
// AssertionError: { a: { b: 1 } } deepEqual { a: { b: 2 } }

assert.deepEqual(obj1, obj3);
// OK

// Prototypes are ignored:
assert.deepEqual(obj1, obj4);
// AssertionError: { a: { b: 1 } } deepEqual {}

如果值不相等,则抛出 AssertionError,其 message 属性设置为等于 message 参数的值。如果未定义 message 参数,则分配默认错误消息。如果 message 参数是 <Error> 的实例,则将抛出该参数而不是 AssertionError

¥If the values are not equal, an AssertionError is thrown with a message property set equal to the value of the message parameter. If the message parameter is undefined, a default error message is assigned. If the message parameter is an instance of <Error> then it will be thrown instead of the AssertionError.