From c310c6939d030d18ae35e2638d7983115e859c1b Mon Sep 17 00:00:00 2001 From: chengjieyun59 Date: Mon, 1 Mar 2021 17:22:10 -0800 Subject: [PATCH 1/3] clarify Object.is() behavior as compared to === operator --- .../global_objects/object/is/index.html | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/object/is/index.html b/files/en-us/web/javascript/reference/global_objects/object/is/index.html index 3b363828cf10f31..e46d486b4a42b6f 100644 --- a/files/en-us/web/javascript/reference/global_objects/object/is/index.html +++ b/files/en-us/web/javascript/reference/global_objects/object/is/index.html @@ -68,32 +68,39 @@

Description

This is also not the same as being equal according to the {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}} operator. The - === operator (and the == operator as well) treats the number - values -0 and +0 as equal and treats {{jsxref("Number.NaN")}} - as not equal to {{jsxref("NaN")}}.

+ only difference between Object.is() and === is in + their treatment of signed zeroes and NaNs. For example, the === + operator (and the == operator) treats the number values -0 + and +0 as equal. Also, the === operator treats + {{jsxref("Number.NaN")}} and {{jsxref("NaN")}} as not equal.

Examples

Using Object.is

-
Object.is('foo', 'foo');     // true
-Object.is(window, window);   // true
-
-Object.is('foo', 'bar');     // false
-Object.is([], []);           // false
-
+
// Case 1: Evaluation result is the same as using ===
+Object.is(25, 25);                // true
+Object.is('foo', 'foo');          // true
+Object.is('foo', 'bar');          // false
+Object.is(null, null);            // true
+Object.is(undefined, undefined);  // true
+Object.is(window, window);        // true
+Object.is([], []);                // false
 var foo = { a: 1 };
 var bar = { a: 1 };
-Object.is(foo, foo);         // true
-Object.is(foo, bar);         // false
-
-Object.is(null, null);       // true
-
-// Special Cases
-Object.is(0, -0);            // false
-Object.is(0n, -0n);          // true
-Object.is(-0, -0);           // true
-Object.is(NaN, 0/0);         // true
+Object.is(foo, foo);              // true
+Object.is(foo, bar);              // false
+
+// Case 2: Signed zero
+Object.is(0, -0);                 // false
+Object.is(+0, -0);                // false
+Object.is(-0, -0);                // true
+const n = 15;
+Object.is(0n, -0n);               // true
+
+// Case 3: NaN
+Object.is(NaN, 0/0);              // true
+Object.is(NaN, Number.NaN)        // true
 

Polyfill

From e45edd48542a5689163b62dac205662463c524fb Mon Sep 17 00:00:00 2001 From: chengjieyun59 Date: Mon, 1 Mar 2021 17:22:59 -0800 Subject: [PATCH 2/3] explain Polyfill code and match case numbers to former section --- .../reference/global_objects/object/is/index.html | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/object/is/index.html b/files/en-us/web/javascript/reference/global_objects/object/is/index.html index e46d486b4a42b6f..736f7bfac036b16 100644 --- a/files/en-us/web/javascript/reference/global_objects/object/is/index.html +++ b/files/en-us/web/javascript/reference/global_objects/object/is/index.html @@ -109,11 +109,16 @@

Polyfill

  Object.defineProperty(Object, "is", {   value: function (x, y) { // SameValue algorithm - if (x === y) { // Steps 1-5, 7-10 - // Steps 6.b-6.e: +0 != -0 + if (x === y) { + // return true if x and y are not 0, OR + // if x and y are both 0 of the same sign. + // This checks for cases 1 and 2. return x !== 0 || 1 / x === 1 / y; } else { - // Step 6.a: NaN == NaN + // return true if both x AND y evaluates to NaN. + // The only possibility for a variable to not be strictly equal to itself + // is when that variable evaluates to NaN (Ex: Number.NaN, 0/0, NaN) + // This checks for case 3 return x !== x && y !== y; }   } From a9366eef64ef02ea7507182a4d6b3e47b7e85f7d Mon Sep 17 00:00:00 2001 From: Catherine Cheng Date: Mon, 1 Mar 2021 21:12:09 -0800 Subject: [PATCH 3/3] Fix spellings from code review Co-authored-by: Michael[tm] Smith --- .../reference/global_objects/object/is/index.html | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/files/en-us/web/javascript/reference/global_objects/object/is/index.html b/files/en-us/web/javascript/reference/global_objects/object/is/index.html index 736f7bfac036b16..2f73243b3634c5e 100644 --- a/files/en-us/web/javascript/reference/global_objects/object/is/index.html +++ b/files/en-us/web/javascript/reference/global_objects/object/is/index.html @@ -112,13 +112,13 @@

Polyfill

if (x === y) { // return true if x and y are not 0, OR // if x and y are both 0 of the same sign. - // This checks for cases 1 and 2. + // This checks for cases 1 and 2 above. return x !== 0 || 1 / x === 1 / y; } else { - // return true if both x AND y evaluates to NaN. + // return true if both x AND y evaluate to NaN. // The only possibility for a variable to not be strictly equal to itself - // is when that variable evaluates to NaN (Ex: Number.NaN, 0/0, NaN) - // This checks for case 3 + // is when that variable evaluates to NaN (example: Number.NaN, 0/0, NaN). + // This checks for case 3. return x !== x && y !== y; }   }