From dd178513d2fa8fd83d6eeeaf53540db19b8051d8 Mon Sep 17 00:00:00 2001 From: RShields Date: Mon, 17 Mar 2025 07:58:29 -0700 Subject: [PATCH] Make Object.prototype not equal null --- assert/equal.ts | 4 +--- assert/equal_test.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/assert/equal.ts b/assert/equal.ts index 20146e96076e..4091ab66e00a 100644 --- a/assert/equal.ts +++ b/assert/equal.ts @@ -9,9 +9,7 @@ function isKeyedCollection(x: unknown): x is KeyedCollection { function prototypesEqual(a: object, b: object) { const pa = Object.getPrototypeOf(a); const pb = Object.getPrototypeOf(b); - return pa === pb || - pa === Object.prototype && pb === null || - pa === null && pb === Object.prototype; + return pa === pb; } function isBasicObjectOrArray(obj: object) { diff --git a/assert/equal_test.ts b/assert/equal_test.ts index c068e04aefda..0179cdd07142 100644 --- a/assert/equal_test.ts +++ b/assert/equal_test.ts @@ -383,6 +383,18 @@ Deno.test("equal() with constructor and prototype", async (t) => { }); }); +Deno.test("equal() considers object with prototype Object.prototype not equal to object with no prototype", () => { + const a = Object.create(Object.prototype); + const b = Object.create(null); + assertFalse(equal(a, b)); + assertFalse(equal(b, a)); + + // ensure the only difference is the prototype + Object.setPrototypeOf(b, Object.prototype); + assert(equal(a, b)); + assert(equal(b, a)); +}); + Deno.test("equal() with dynamic properties defined on the prototype", async (t) => { await t.step("built-in web APIs", async (t) => { await t.step("URLPattern", () => {