Skip to content

Instantly share code, notes, and snippets.

@shikelong
Created June 29, 2020 06:46
Show Gist options
  • Save shikelong/9a40823269c10af14cb39203adbe5d64 to your computer and use it in GitHub Desktop.
Save shikelong/9a40823269c10af14cb39203adbe5d64 to your computer and use it in GitHub Desktop.
Short-Utils
//Write a function called strictEquals(a, b) that returns the same value as a === b. Your implementation must not use the === or !== operators.
function strictEquals(a, b){
if (Number.isNaN(a) || Number.isNaN(b)){
return false;
}
//+0/-0
if (isEqual(a, -0) && isEqual(b, +0) || isEqual(a, +0) && isEqual(b, -0)){
return true;
}
return Object.is(a, b);
};
function isEqual(x, y) {
var obj = {};
Object.defineProperty(obj, 'z', { value: x, configurable: false });
try {
// 如果x的值和z属性的当前值不相等的话,就会抛出异常.
Object.defineProperty(obj, 'z', { value: y });
} catch (e) {
return false
};
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment