Actionscript 3 中的 parseInt 与 NaN 的比较
AS3 文档指出,如果您向 parseInt
传递一个不是数字的字符串,它将返回 NaN
。但是,当我尝试与 NaN
进行比较时,编译器给出以下错误:
Warning: 1098: Illogical comparison with NaN. This statement always evaluates to false.
该语句实际上是正确的。与 NaN 相比将始终返回 false
。如何与 NaN 进行比较来检测解析的内容是否为 NaN?
if( parseInt("test") == NaN )
{
// do something (never gets here)
}
The AS3 documentation states that if you pass in a string to parseInt
that is not a number it will return NaN
. However, when I try to compare to NaN
the compiler gives me the following error:
Warning: 1098: Illogical comparison with NaN. This statement always evaluates to false.
The statement is actually true. Comparing to NaN will always return false
. How can I compare to NaN to detect if what was parsed was NaN?
if( parseInt("test") == NaN )
{
// do something (never gets here)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与
isNaN()
函数进行比较。Compare with
isNaN()
function.使用 isNaN() 全局函数
Use isNaN() global function
大家都是对的,使用
isNaN()
函数:http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/package.html#isNaN()
我从来没有真正喜欢过这种方法。我更喜欢测试阳性。
但有趣的是,如果一个 Number 是 NaN,那么它不等于它自己:
因此,测试 Number 是否等于它自己应该会给你积极的结果:
阅读代码时,你的意图并不是很清楚,所以如果你使用的话一定要评论一下。
或者您可以添加一个顶级函数:
以及关于优化坚果的注释。内联版本最快。
仅供参考: http://www.adobe.com /livedocs/flash/9.0/ActionScriptLangRefV3/package.html#parseInt()
Everyone is correct, use the
isNaN()
function:http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/package.html#isNaN()
I've never really liked this method. I prefer to test for positives.
Interestingly though, if a Number is NaN then it will not equate to itself:
Therefor, testing if the Number equates to itself should give you the positive:
It's not very clear what your intentions are when reading the code, so be sure to comment it if you use it.
Or you could add a top-level function:
And a note for the optimisation nuts out there. The inline version is fastest.
Just for reference: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/package.html#parseInt()