0%

手写Object.is

1
2
3
4
5
6
7
8
9
10
11
is=function(x,y){
if(x===y){//如果x===y并且+0和-0=>+0!==0false -0!==0false 1/+0===1/-0false返回false
return x!==0||y!==0||1/x===1/y
}else{
//解决NaN===NaN为false,NaN!==NaNtrue NaN!==NaN true 返回true
return x!==x&&y!==y;
}
}
console.log(is(0,0))//true
console.log(is(+0,-0))//false
console.log(is(NaN,NaN))//true