===
is called strict comparison operator in JavaScript. Object.is
and strict comparison operator behave exactly the same except for NaN
and +0/-0
.
From MDN:
Object.is()
method is not the same as being equal according to the ===
operator. The ===
operator (and the ==
operator as well) treats the number values -0 and +0 as equal and treats Number.NaN
as not equal to NaN
.
Code below highlights the difference between ===
and 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
You can find more examples here.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…