Javascript变量/'NaN'表现异常 [英] Javascript variable / 'NaN' behaving weirdly

查看:75
本文介绍了Javascript变量/'NaN'表现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个文本框,如果文本框中包含有效数据,则返回javascript Number值,否则返回NaN.我得到这种奇怪的行为.当我签入firebug时(两个文本框均为空白):

I have a couple of textboxes, Which return javascript Number values if valid data is in the textboxes, otherwise NaN. I get this strange behavior. When I checked in firebug (both the textboxes are blank):

>>> hours
NaN
>>> minutes
NaN
>>> minutes == NaN
false
>>> hours == NaN
false
>>> hours == minutes
false

为什么会这样?

推荐答案

NaN不等于任何东西,甚至不等于NaN.

NaN is not equal to anything, not even NaN.

在MDN上的引用

更详细的SO问答

有关权威信息,请参见 ECMAScript 5官方规范 11.9.1 11.9.3 :

For the authoritative source, see the ECMAScript 5 Official Specification, sections 11.9.1 and 11.9.3:

1. If Type(x) is the same as Type(y), then
     [...]
  c. If Type(x) is Number, then
     i. If x is NaN, return false.
    ii. If y is NaN, return false.
        [...]

这篇关于Javascript变量/'NaN'表现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆