Number.IsNaN()比isNaN()更破碎 [英] Is Number.IsNaN() more broken than isNaN()
问题描述
isNaN('')
isNaN('')
isNaN(true)
isNaN(false)
isNaN([0])
返回false,当它们看起来都是...不是数字...
在ECMAScript 6中,草稿包含一个新的 Number.isNaN
,但它看起来像(imo)这也是坏的...
我希望
Number.isNaN('RAWRRR')
要返回true,因为它是一个字符串,不能转换为一个数字...但是...
似乎我会考虑的事情...不是数字,确实不是,不是数字...
http://people.mozilla.org/~ jorendorff / es6-draft.html#sec-isfinite-number
MDN上的示例说:
Number.isNaN(blabla); //例如这将是真实的isNaN
我不明白这是如何更健壮的版本的原始全球是NNN。当我无法查看是否有事情不是一个数字。
这意味着我们仍然需要进行实际的类型检查以及检查isNaN ...哪个似乎很傻...
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-isnan-number
这里的ES3草案基本上都说,一切都是假的,除了它的Number.NaN
有没有人会发现这是坏的还是我只是不了解isNaN的观点?
isNaN()
和 isNaN()
都测试一个值是(或者,在 isNaN()
的情况下,可以转换为数字类型的值,代表) NaN
值。换句话说,NaN不仅仅意味着这个值不是一个数字,它具体意味着这个值是符合IEEE-754的数字非数字值。 / p>
上面所有的测试返回false的原因是因为所有给定的值都可以转换为不是 NaN $ c的数值$ c>:
数字('')// 0
数字('')// 0
Number(true)// 1
Number(false)// 0
数字([0])// 0
原因 isNaN()
是打破的原因是表面上,类型转换不应该在测试值时发生。这就是问题 Number.isNaN()
旨在解决。特别地, Number.isNaN()
将仅尝试将值与 NaN
如果该值是数字类型值。任何其他类型都将返回false,即使它们是字面上的不是数字,因为值 NaN
的类型是数字。请参阅 isNaN的相关MDN文档()
和 Number.isNaN()
。
如果您只想确定是否值为数字类型,即使该值为 NaN
,请使用 typeof
而是:
typeof'RAWRRR'==='number'// false
Soooooo isNaN
is apparently broken in JavaScript, with things like:
isNaN('')
isNaN(' ')
isNaN(true)
isNaN(false)
isNaN([0])
Returning false, when they appear to all be... Not a Number...
In ECMAScript 6, the draft includes a new Number.isNaN
but it looks like (imo) that this is also broken...
I would expect
Number.isNaN('RAWRRR')
To return true, since it's a string, and cannot be converted to a number... However...
It seems that things that I would consider... not a number, are indeed, not, not a number...
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-isfinite-number
The examples on MDN say:
Number.isNaN("blabla"); // e.g. this would have been true with isNaN
I don't understand how this is "More robust version of the original global isNaN." when I cannot check to see if things are not a number.
This would mean we're still subjected to doing actual type checking as well as checking isNaN... which seems silly...
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-isnan-number
The ES3 draft here basically says, everything is always false, except with its Number.NaN
Does anyone else find this is broken or am I just not understanding the point of isNaN?
isNaN()
and Number.isNaN()
both test if a value is (or, in the case of isNaN()
, can be converted to a number-type value that represents) the NaN
value. In other words, "NaN" does not simply mean "this value is not a number", it specifically means "this value is a numeric Not-a-Number value according to IEEE-754".
The reason all your tests above return false is because all of the given values can be converted to a numeric value that is not NaN
:
Number('') // 0
Number(' ') // 0
Number(true) // 1
Number(false) // 0
Number([0]) // 0
The reason isNaN()
is "broken" is because, ostensibly, type conversions aren't supposed to happen when testing values. That is the issue Number.isNaN()
is designed to address. In particular, Number.isNaN()
will only attempt to compare a value to NaN
if the value is a number-type value. Any other type will return false, even if they are literally "not a number", because the type of the value NaN
is number. See the respective MDN docs for isNaN()
and Number.isNaN()
.
If you simply want to determine whether or not a value is of the number type, even if that value is NaN
, use typeof
instead:
typeof 'RAWRRR' === 'number' // false
这篇关于Number.IsNaN()比isNaN()更破碎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!