Skip to content

Instantly share code, notes, and snippets.

@guimochila
Created March 8, 2021 14:48
Show Gist options
  • Save guimochila/0433a36d1f7a947e2d4268f8c100eec9 to your computer and use it in GitHub Desktop.
Save guimochila/0433a36d1f7a947e2d4268f8c100eec9 to your computer and use it in GitHub Desktop.
Object.is basic polyfill
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