为什么JavaScript说数字不是数字? [英] Why JavaScript says that a number is not a number?
问题描述
我有一段JavaScript代码,预计会为变量设置一个整数值。
I have a piece of JavaScript code which is expected to set an integer value to a variable.
有些东西坏了,所以当我尝试<$ c时$ c> alert(A); ,它返回 NaN
。 isNaN(A);
返回true。但如果我 alert(typeof(A));
,则说 number
。
Something is broken, so when I try to do alert(A);
, it returns NaN
. isNaN(A);
returns true. But if I alert(typeof(A));
, it says number
.
那么变量怎么可以是一个数字而不是一个数字呢?也许我误解了NaN究竟是什么?
So how can a variable be a number and not a number at the same time? Maybe I misunderstood what NaN really is?
编辑:感谢答案,我明白了我错了,因为:
thanks to the answers, I see that I was wrong, because:
-
NaN
的类型是数字
, -
NaN
的意思是不是数字,这是不一样的不是类型数字
, -
0/0
是NaN
的一个很好的例子:它仍然是一个数字,但JavaScript(没有其他人)可以说零的实际值除以零。另一方面,1/0
返回Infinity
,这不是NaN
。
- The type of
NaN
isNumber
, NaN
does mean "Not a number", which is not the same thing as "not of typeNumber
",0/0
is a good example ofNaN
: it is still a number, but JavaScript (and nobody else) can say what is the real value of zero divided by zero.1/0
on the other hand returnsInfinity
, which is notNaN
.
推荐答案
据我了解, NaN
是 Number
类的一个标记实例,它代表了它所代表的 - 无法充分表示的数字结果。所以 0/0
不是一个数字,因为它是 NaN
,但它是一个数字
就其类型而言。
As I understand it, NaN
is a sentinel instance of the Number
class that represents, well, exactly what it stands for - numeric results that cannot be adequately represented. So 0/0
is not a number, in the sense that it's NaN
, but it is a Number
in terms of its type.
也许它应该被称为NaRN(不是可表示的数字) )。
Perhaps it should have been called NaRN (Not a Representable Number).
这篇关于为什么JavaScript说数字不是数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!