为什么JavaScript说数字不是数字? [英] Why JavaScript says that a number is not a number?

查看:201
本文介绍了为什么JavaScript说数字不是数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段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 is Number,
  • NaN does mean "Not a number", which is not the same thing as "not of type Number",
  • 0/0 is a good example of NaN: 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 returns Infinity, which is not NaN.

推荐答案

据我了解, 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屋!

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