比较详情
¥Comparison details
-
使用
Object.is()
比较原始值。¥Primitive values are compared using
Object.is()
. -
对象的 类型标签 应该是相同的。
¥Type tags of objects should be the same.
-
不比较对象的
[[Prototype]]
。¥
[[Prototype]]
of objects are not compared. -
仅考虑 可枚举的 "自有" 属性。
¥Only enumerable "own" properties are considered.
-
始终比较 <Error> 名称、消息、原因和错误,即使这些不是可枚举属性。
errors
也进行了比较。¥<Error> names, messages, causes, and errors are always compared, even if these are not enumerable properties.
errors
is also compared. -
也比较了可枚举的自有 Symbol 属性。
¥Enumerable own Symbol properties are compared as well.
-
对象封装器 作为对象和展开的值进行比较。
¥Object wrappers are compared both as objects and unwrapped values.
-
Object
属性是无序比较的。¥
Object
properties are compared unordered. -
当双方不同或双方遇到循环引用时,则递归停止。
¥Recursion stops when both sides differ or both sides encounter a circular reference.
-
WeakMap、WeakSet 和 <Promise> 实例在结构上不进行比较。只有当它们引用同一个对象时,它们才是相等的。任何不同的
WeakMap
、WeakSet
或Promise
实例之间的比较都会导致不相等,即使它们包含相同的内容。¥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
, orPromise
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.
-
稀疏数组中的漏洞将被忽略。
¥Holes in sparse arrays are ignored.
import assert from 'node:assert';
assert.partialDeepStrictEqual(
{ a: { b: { c: 1 } } },
{ a: { b: { c: 1 } } },
);
// OK
assert.partialDeepStrictEqual(
{ a: 1, b: 2, c: 3 },
{ b: 2 },
);
// OK
assert.partialDeepStrictEqual(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 8],
);
// OK
assert.partialDeepStrictEqual(
new Set([{ a: 1 }, { b: 1 }]),
new Set([{ a: 1 }]),
);
// OK
assert.partialDeepStrictEqual(
new Map([['key1', 'value1'], ['key2', 'value2']]),
new Map([['key2', 'value2']]),
);
// OK
assert.partialDeepStrictEqual(123n, 123n);
// OK
assert.partialDeepStrictEqual(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[5, 4, 8],
);
// AssertionError
assert.partialDeepStrictEqual(
{ a: 1 },
{ a: 1, b: 2 },
);
// AssertionError
assert.partialDeepStrictEqual(
{ a: { b: 2 } },
{ a: { b: '2' } },
);
// AssertionError
const assert = require('node:assert');
assert.partialDeepStrictEqual(
{ a: { b: { c: 1 } } },
{ a: { b: { c: 1 } } },
);
// OK
assert.partialDeepStrictEqual(
{ a: 1, b: 2, c: 3 },
{ b: 2 },
);
// OK
assert.partialDeepStrictEqual(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[4, 5, 8],
);
// OK
assert.partialDeepStrictEqual(
new Set([{ a: 1 }, { b: 1 }]),
new Set([{ a: 1 }]),
);
// OK
assert.partialDeepStrictEqual(
new Map([['key1', 'value1'], ['key2', 'value2']]),
new Map([['key2', 'value2']]),
);
// OK
assert.partialDeepStrictEqual(123n, 123n);
// OK
assert.partialDeepStrictEqual(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[5, 4, 8],
);
// AssertionError
assert.partialDeepStrictEqual(
{ a: 1 },
{ a: 1, b: 2 },
);
// AssertionError
assert.partialDeepStrictEqual(
{ a: { b: 2 } },
{ a: { b: '2' } },
);
// AssertionError