比较详情
\Comparison details
-
使用
Object.is()比较原始值。\Primitive values are compared using
Object.is(). -
对象的 类型标签 应该是相同的。
\Type tags of objects should be the same.
-
使用
===操作符 比较对象的[[Prototype]]。\
[[Prototype]]of objects are compared using the===operator. -
仅考虑 可枚举的 "自有" 属性。
\Only enumerable "own" properties are considered.
-
始终比较
Error名称、消息、原因和错误,即使这些不是可枚举属性。errors也进行了比较。\
Errornames, messages, causes, and errors are always compared, even if these are not enumerable properties.errorsis also compared. -
也比较了可枚举的自有
Symbol属性。\Enumerable own
Symbolproperties are compared as well. -
对象封装器 作为对象和展开的值进行比较。
\Object wrappers are compared both as objects and unwrapped values.
-
Object属性是无序比较的。\
Objectproperties are compared unordered. -
当双方不同或双方遇到循环引用时,则递归停止。
\Recursion stops when both sides differ or both sides encounter a circular reference.
-
WeakMap和WeakSet实例未进行结构比较。只有当它们引用同一个对象时,它们才是相等的。不同WeakMap或WeakSet实例之间的任何比较都会导致不相等,即使它们包含相同的条目。\
WeakMapandWeakSetinstances are not compared structurally. They are only equal if they reference the same object. Any comparison between differentWeakMaporWeakSetinstances will result in inequality, even if they contain the same entries. -
RegExp的 lastIndex、flags 和 source 总是被比较,即使它们不是可枚举的属性。\
RegExplastIndex, flags, and source are always compared, even if these are not enumerable properties.
import assert from 'node:assert/strict'; // This fails because 1 !== '1'. assert.deepStrictEqual({ a: 1 }, { a: '1' }); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // { // + a: 1 // - a: '1' // } // The following objects don't have own properties const date = new Date(); const object = {}; const fakeDate = {}; Object.setPrototypeOf(fakeDate, Date.prototype); // Different [[Prototype]]: assert.deepStrictEqual(object, fakeDate); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + {} // - Date {} // Different type tags: assert.deepStrictEqual(date, fakeDate); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + 2018年04月26日T00:49:08.604Z // - Date {} assert.deepStrictEqual(NaN, NaN); // OK because Object.is(NaN, NaN) is true. // Different unwrapped numbers: assert.deepStrictEqual(new Number(1), new Number(2)); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + [Number: 1] // - [Number: 2] assert.deepStrictEqual(new String('foo'), Object('foo')); // OK because the object and the string are identical when unwrapped. assert.deepStrictEqual(-0, -0); // OK // Different zeros: assert.deepStrictEqual(0, -0); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + 0 // - -0 const symbol1 = Symbol(); const symbol2 = Symbol(); assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 }); // OK, because it is the same symbol on both objects. assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal: // // { // [Symbol()]: 1 // } const weakMap1 = new WeakMap(); const weakMap2 = new WeakMap(); const obj = {}; weakMap1.set(obj, 'value'); weakMap2.set(obj, 'value'); // Comparing different instances fails, even with same contents assert.deepStrictEqual(weakMap1, weakMap2); // AssertionError: Values have same structure but are not reference-equal: // // WeakMap { // <items unknown> // } // Comparing the same instance to itself succeeds assert.deepStrictEqual(weakMap1, weakMap1); // OK const weakSet1 = new WeakSet(); const weakSet2 = new WeakSet(); weakSet1.add(obj); weakSet2.add(obj); // Comparing different instances fails, even with same contents assert.deepStrictEqual(weakSet1, weakSet2); // AssertionError: Values have same structure but are not reference-equal: // + actual - expected // // WeakSet { // <items unknown> // } // Comparing the same instance to itself succeeds assert.deepStrictEqual(weakSet1, weakSet1); // OKconst assert = require('node:assert/strict'); // This fails because 1 !== '1'. assert.deepStrictEqual({ a: 1 }, { a: '1' }); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // { // + a: 1 // - a: '1' // } // The following objects don't have own properties const date = new Date(); const object = {}; const fakeDate = {}; Object.setPrototypeOf(fakeDate, Date.prototype); // Different [[Prototype]]: assert.deepStrictEqual(object, fakeDate); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + {} // - Date {} // Different type tags: assert.deepStrictEqual(date, fakeDate); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + 2018年04月26日T00:49:08.604Z // - Date {} assert.deepStrictEqual(NaN, NaN); // OK because Object.is(NaN, NaN) is true. // Different unwrapped numbers: assert.deepStrictEqual(new Number(1), new Number(2)); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + [Number: 1] // - [Number: 2] assert.deepStrictEqual(new String('foo'), Object('foo')); // OK because the object and the string are identical when unwrapped. assert.deepStrictEqual(-0, -0); // OK // Different zeros: assert.deepStrictEqual(0, -0); // AssertionError: Expected inputs to be strictly deep-equal: // + actual - expected // // + 0 // - -0 const symbol1 = Symbol(); const symbol2 = Symbol(); assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 }); // OK, because it is the same symbol on both objects. assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 }); // AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal: // // { // [Symbol()]: 1 // } const weakMap1 = new WeakMap(); const weakMap2 = new WeakMap(); const obj = {}; weakMap1.set(obj, 'value'); weakMap2.set(obj, 'value'); // Comparing different instances fails, even with same contents assert.deepStrictEqual(weakMap1, weakMap2); // AssertionError: Values have same structure but are not reference-equal: // // WeakMap { // <items unknown> // } // Comparing the same instance to itself succeeds assert.deepStrictEqual(weakMap1, weakMap1); // OK const weakSet1 = new WeakSet(); const weakSet2 = new WeakSet(); weakSet1.add(obj); weakSet2.add(obj); // Comparing different instances fails, even with same contents assert.deepStrictEqual(weakSet1, weakSet2); // AssertionError: Values have same structure but are not reference-equal: // + actual - expected // // WeakSet { // <items unknown> // } // Comparing the same instance to itself succeeds assert.deepStrictEqual(weakSet1, weakSet1); // OK
如果值不相等,则抛出 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.