• 使用 [SameValue比较][SameValue Comparison](使用 [Object.is()])来比较原始值。
    • 对象的[类型标签][Object.prototype.toString()]应该相同。
    • 使用[严格相等比较][Strict Equality Comparison]来比较对象的[原型][prototype-spec]。
    • 只考虑[可枚举的自身属性][enumerable “own” properties]。
    • 始终比较 [Error] 的名称和消息,即使这些不是可枚举的属性。
    • 可枚举的自身 [Symbol] 属性也会比较。
    • [对象封装器][Object wrappers]作为对象和解封装后的值都进行比较。
    • Object 属性的比较是无序的。
    • [Map] 键名与 [Set] 子项的比较是无序的。
    • 当两边的值不相同或遇到循环引用时,递归停止。
    • [WeakMap] 和 [WeakSet] 的比较不依赖于它们的值。请参见下文了解更多详情。
    1. const assert = require('assert').strict;
    2. // 失败,因为 1 !== '1'。
    3. assert.deepStrictEqual({ a: 1 }, { a: '1' });
    4. // AssertionError: Expected inputs to be strictly deep-equal:
    5. // + actual - expected
    6. //
    7. // {
    8. // + a: 1
    9. // - a: '1'
    10. // }
    11. // 以下对象没有自身属性。
    12. const date = new Date();
    13. const object = {};
    14. const fakeDate = {};
    15. Object.setPrototypeOf(fakeDate, Date.prototype);
    16. // 原型不同:
    17. assert.deepStrictEqual(object, fakeDate);
    18. // AssertionError: Expected inputs to be strictly deep-equal:
    19. // + actual - expected
    20. //
    21. // + {}
    22. // - Date {}
    23. // 类型标签不同:
    24. assert.deepStrictEqual(date, fakeDate);
    25. // AssertionError: Expected inputs to be strictly deep-equal:
    26. // + actual - expected
    27. //
    28. // + 2018-04-26T00:49:08.604Z
    29. // - Date {}
    30. assert.deepStrictEqual(NaN, NaN);
    31. // 通过,因为使用 SameValue 比较。
    32. // 解封装后的数字不同:
    33. assert.deepStrictEqual(new Number(1), new Number(2));
    34. // AssertionError: Expected inputs to be strictly deep-equal:
    35. // + actual - expected
    36. //
    37. // + [Number: 1]
    38. // - [Number: 2]
    39. assert.deepStrictEqual(new String('foo'), Object('foo'));
    40. // 通过,因为对象与解封装后的字符串都是相同的。
    41. assert.deepStrictEqual(-0, -0);
    42. // 通过。
    43. // 使用 SameValue 比较的零不同:
    44. assert.deepStrictEqual(0, -0);
    45. // AssertionError: Expected inputs to be strictly deep-equal:
    46. // + actual - expected
    47. //
    48. // + 0
    49. // - -0
    50. const symbol1 = Symbol();
    51. const symbol2 = Symbol();
    52. assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol1]: 1 });
    53. // 通过,因为在两个对象上的 symbol 相同。
    54. assert.deepStrictEqual({ [symbol1]: 1 }, { [symbol2]: 1 });
    55. // AssertionError [ERR_ASSERTION]: Inputs identical but not reference equal:
    56. //
    57. // {
    58. // [Symbol()]: 1
    59. // }
    60. const weakMap1 = new WeakMap();
    61. const weakMap2 = new WeakMap([[{}, {}]]);
    62. const weakMap3 = new WeakMap();
    63. weakMap3.unequal = true;
    64. assert.deepStrictEqual(weakMap1, weakMap2);
    65. // 通过,因为无法比较条目。
    66. // 失败,因为 weakMap3 有一个 weakMap1 不包含的属性:
    67. assert.deepStrictEqual(weakMap1, weakMap3);
    68. // AssertionError: Expected inputs to be strictly deep-equal:
    69. // + actual - expected
    70. //
    71. // WeakMap {
    72. // + [items unknown]
    73. // - [items unknown],
    74. // - unequal: true
    75. // }

    如果值不相等,则抛出 [AssertionError],并将 message 属性设置为等于 message 参数的值。 如果未定义 message 参数,则会分配默认错误消息。 如果 message 参数是 [Error] 的实例,那么它将被抛出而不是 AssertionError