比较详情
【Comparison details】
- 原始值使用
==运算符 进行比较,NaN除外。如果两边都是NaN,则被视为相同。 - 对象的类型标签应该相同。
- 只有可枚举的“自有”属性被考虑。
- 即使这些不是可枚举属性,
Error的名称和消息也总是会被比较。 - 对象封装器 既作为对象也作为未拆封的值进行比较。
Object属性是无序比较的。Map个键和Set个项目未排序比较。- 当两边不同或两边遇到循环引用时,递归会停止。
- 实现不会测试对象的
[[Prototype]]。 Symbol属性未被比较。WeakMap和WeakSet的比较不依赖于它们的数值。
以下示例不会抛出 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);“深层”平等意味着还会对子对象的可枚举“自身”属性进行评估:
import assert from 'node:assert';
const obj1 = {
a: {
b: 1
}
};
const obj2 = {
a: {
b: 2
}
};
const obj3 = {
a: {
b: 1
}
};
const obj4 = Object.create(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 = Object.create(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 an Error then it will be thrown instead of the
AssertionError.】