development

Object.is vs ===

big-blog 2020. 6. 27. 10:10
반응형

Object.is vs ===


이 비교를 사용하는 코드 예제를 우연히 발견했습니다.

var someVar = 0;
Object.is(false, someVar); //Returns false 

알 수 false == 0있을 것입니다 true우리가 가지고있는 이유의 ===.

Object.is와는 어떻게 다른 ===가요?


===JavaScript에서는 엄격한 비교 연산자라고합니다. Object.is엄격한 비교 연산자는 정확히 제외하고 같은 행동을 NaN하고 +0/-0.

MDN에서 :

Object.is()방법은 ===연산자 에 따라 동일하지 않습니다 . ===연산자 (그리고 ==물론 오퍼레이터)는 숫자 값과 동일 -0 +0로서 취급하고 처리 Number.NaN와 동일하지 NaN.

코드는 아래의 차이 강조 ===Object.is().

console.log(+0 === -0); //true
console.log(Object.is(+0, -0)); //false

console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); //true

console.log(Number.NaN === Number.NaN); // false
console.log(Object.is(Number.NaN, Number.NaN)); // true

console.log(NaN === Number.NaN); // false
console.log(Object.is(NaN, Number.NaN)); // true

여기에 이미지 설명을 입력하십시오

여기에서 더 많은 예제를 찾을 수 있습니다 .

Note: Object.is is part of the ECMAScript 6 proposal and is not widely supported yet (e.g. it's not supported by any version of Internet Explorer or many older versions of other browsers). However you can use a polyfill for non-ES6 browsers which can be found in link given above.


Object.is uses the specification's SameValue algorithm, whereas === uses the Strict Equality Algorithm. A note on the Strict Equality Algorithm calls out the difference:

This algorithm differs from the SameValue Algorithm...in its treatment of signed zeroes and NaNs.

Note that:

  • NaN === NaN is false, but Object.is(NaN, NaN) is true
  • +0 === -0 is true, but Object.is(+0, -0) is false
  • -0 === +0 is true, but Object.is(-0, +0) is false

JavaScript has at least four kinds of "equality":

  • "Loose" (==), where the operands will be coerced to try to make them match. The rules are clearly specified, but non-obvious. ("" == 0 is true; "true" == true is false, ...).
  • "Strict" (===), where operands of differing types will not be coerced (and will not be equal), but see note above about NaN and positive and negative zero.
  • SameValue - as listed above (used by Object.is).
  • SameValueZero - like SameValue except +0 and -0 are the same instead of different (used by Map for keys, and by Array.prototype.includes).

There's also object equivalence, which isn't provided by the language or runtime itself, but is usually expressed as: The objects have the same prototype, same properties, and their property values are the same (by some reasonable definition of "the same").


SameValue algorithm:

  • If Type(x) is different from Type(y), return false.
  • If Type(x) is Number, then
    • If x is NaN and y is NaN, return true.
    • If x is +0 and y is -0, return false.
    • If x is -0 and y is +0, return false.
    • If x is the same Number value as y, return true.
    • Return false.
  • Return SameValueNonNumber(x, y).

...where SameValueNonNumber is:

  • Assert: Type(x) is not Number.
  • Assert: Type(x) is the same as Type(y).
  • If Type(x) is Undefined, return true.
  • If Type(x) is Null, return true.
  • If Type(x) is String, then
    • If x and y are exactly the same sequence of code units (same length and same code units at corresponding indices), return true; otherwise, return false.
  • If Type(x) is Boolean, then
    • If x and y are both true or both false, return true; otherwise, return false.
  • If Type(x) is Symbol, then
    • If x and y are both the same Symbol value, return true; otherwise, return false.
  • Return true if x and y are the same Object value. Otherwise, return false.

Strict Equality Algorithm:

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number, then
    • If x is NaN, return false.
    • If y is NaN, return false.
    • If x is the same Number value as y, return true.
    • If x is +0 and y is -0, return true.
    • If x is -0 and y is +0, return true.
    • Return false.
  3. Return SameValueNonNumber(x, y).

Object.is = function(v1, v2){
  //test for `-0`
  if(v1 === 0 && v2 === 0) {
    return 1 / v1 === 1 / v2;
  }
  
  //test for `NaN`
  if(v1 !== v1) {
    return v2 !== v2;
  }
  
  //everything else
  return v1 === v2;
}

The above is the polyfill function to show how Object.is works, for anyone who are interested to know. A reference to You-Don't-Know-JS


Summary:

The Object.is() function takes 2 values as arguments and returns true if the 2 given values are exact the same, otherwise it will return false.

Why do we need this?

You might think, we already have strict equality (checks type + value) checking in javascript with the === operator, why do we need this function? Well strict equality isn't sufficient in some cases and they are the following:

console.log(NaN === NaN);   // false
console.log(-0 === +0);     // true

Object.is() helps us by being able to compare these values to see if they are similar, something the strict equality operator cannot do.

console.log(Object.is(NaN, NaN));  // true
console.log(Object.is(-0, 0));     // false
console.log(Object.is(+0, +0));    // true
console.log(Object.is(+0, -0));    // false

참고URL : https://stackoverflow.com/questions/30543190/object-is-vs

반응형