⭐ ⭐

Difficulty ratings range from five stars to one star.

A:

The object. is method is used to compare whether two values are equal. Pass in two arguments and return a Boolean value.

This method fixes the misjudgment of === in special cases, such as +0 and -0, NaN and NaN.

0= = = -0     // true is supposed to be false
NaN= = =NaN  // False is supposed to be true
Copy the code
/ / Object is
Object.is(0, -0)    // false
Object.is(NaN.NaN) // true
Copy the code

Compatibility is pretty good, except for Internet Explorer.

Polyfill

From Polyfill, analyze how Object. Is solves the problem of ===.

if (!Object.is) {
  Object.defineProperty(Object.'is', {
    value: function (x, y) {
      if (x === y) {
        // x is 0, y is -0, the program goes here
        // 1/+0 = +Infinity, 1/-0 = -infinity, return +Infinity === -infinity, false
        returnx ! = =0 || 1 / x === 1 / y
      } else {
        // if x and y are NaN, this is where the program goes
        // Return NaN! == NaN && NaN ! == NaN, true
        returnx ! == x && y ! == y } } }) }Copy the code

At the end

This is the 34th day of alynn’s blog post, exporting insight techniques. Goodbye

If my article is helpful to you, your 👍 is my biggest support ^_^

Related issues:

What is the difference between “front-end daily question (6)” == and ===?