如何测试某些东西是否与NaN相同? [英] How to test whether something is identically NaN?
问题描述
在Javascript中:
In Javascript:
NaN === NaN; // false
我试图确定何时 isNaN(foo)
不仅仅等于 + foo
是 NaN
。但是我不知道如何判断某些东西是否是 NaN
,除非使用 isNaN
,这对许多事情都是肯定的,其中没有 ===
NaN。
I was trying to determine when isNaN(foo)
is not just equivalent to +foo
"is" NaN
. But I don't know how to tell if something is NaN
except by using isNaN
, which says yes for many things, none of which ===
NaN.
所以我认为正确的方法是工作围绕其他可能性:
So I think the right way to do this would be to work around other possibilities:
typeof NaN === 'number' // true
因此我认为
typeof(foo) === 'number' && isNaN(foo)
最接近我的想法。这是有道理的,因为 NaN
是唯一不是数字或其他东西的数字。这是正确的,这是最好的方法吗?
Is the closest to what I am thinking of. It makes sense since it makes sense that NaN
would be the only number that isn't a number, or something. Is this correct, and is this the best way?
推荐答案
利用这个优势:
foo !== foo
这相当于 typeof foo ==='number'&& isNaN(foo)
,是Underscore.js用于检查确切 NaN
的内容。
This is equivalent to typeof foo === 'number' && isNaN(foo)
, and is what Underscore.js uses to check for exactly NaN
, too.
这篇关于如何测试某些东西是否与NaN相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!