We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用 API: Object.is() 方法判断两个值是否为同一个值
Object.is()
Object.is(x, y)
Polyfill:
if (!Object.is) { Object.is = function(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7-10 // Steps 6.b-6.e: +0 != -0 return x !== 0 || 1 / x === 1 / y; } else { // Step 6.a: NaN == NaN return x !== x && y !== y; } }; }
JavaScript提供三种不同的值比较操作:
Object.is
其中:
NaN
-0
+0
Object.is(NaN, NaN)
true
Object.is(+0, -0)
false
The text was updated successfully, but these errors were encountered:
避免+0,-0,1/-Infinity,1/Infinity都相等。
function is(x, y) { if (x === y) { return x !== 0 || y !== 0 || 1 / x === 1 / y; } else { return x !== x && y !== y; } }
Sorry, something went wrong.
Object.is(value1, value2);
/** * https://www.cnblogs.com/lindasu/p/7471519.html * === Object.is * https://github.com/sisterAn/JavaScript-Algorithms/issues/116 */ Object._is = function(x,y) { if(x===y) { return x!==0||1/x===1/y } else { return x!==x&&y!==y } } console.log(Object._is(NaN,NaN))
No branches or pull requests
使用 API:
Object.is()
方法判断两个值是否为同一个值Polyfill:
扩展:
JavaScript提供三种不同的值比较操作:
Object.is
(ECMAScript 2015/ ES6 新特性):同值相等其中:
Object.is
:与 === 相同,但是对于NaN
和-0
和+0
进行特殊处理,Object.is(NaN, NaN)
为true
,Object.is(+0, -0)
为false
The text was updated successfully, but these errors were encountered: