Created
March 8, 2021 14:48
-
-
Save guimochila/0433a36d1f7a947e2d4268f8c100eec9 to your computer and use it in GitHub Desktop.
Object.is basic polyfill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Object.is || true) { | |
Object.is = function ObjectIs(x, y) { | |
var xNegZero = isItNegZero(x); | |
var yNegZero = isItNegZero(y); | |
if(xNegZero || yNegZero) { | |
return xNegZero && yNegZero; | |
} else if (isItNaN(x) && isItNaN(y)) { | |
return true; | |
} else { | |
return x === y; | |
} | |
function isItNegZero(v) { | |
return v === 0 && (1/v) === -Infinity; | |
} | |
function isItNaN(v) { | |
return v !== v; | |
} | |
} | |
} | |
console.log(Object.is(42,42) === true); | |
console.log(Object.is(NaN, NaN) === true); | |
console.log(Object.is(-0,-0) === true); | |
console.log(Object.is(42, "42") === false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment