区分+0和-0 [英] Differentiating +0 and -0
本文介绍了区分+0和-0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
事实证明 +0 === -0
评估为 true
尽管 + 0
和 -0
是不同的实体。那么,你如何从 -0
中区分 +0
?
It turns out +0 === -0
evaluates to true
despite +0
and -0
being different entities. So, how do you differentiate +0
from -0
?
有一个黑客:
if (1 / myZero > 0) {
// myZero is +0
} else {
// myZero is -0
}
我可以做得更好吗?
推荐答案
在ECMAScript 6中 Object.is
表现得像 ===
,除了它区分正负零和 Object.is(NaN,NaN)
评估为 true
。 (请参阅此处进行撰写。)
In ECMAScript 6 Object.is
behaves like ===
except that it distinguishes positive and negative zeroes, and Object.is(NaN, NaN)
evaluates to true
. (See here for a writeup.)
Chrome 24支持 Object.is
。
Chrome 24 supports Object.is
.
这篇关于区分+0和-0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文