Javascript $.isNumeric 与 isNaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8012807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$.isNumeric vs. isNaN
提问by Phillip Senn
Is there any difference between $.isNumericand !isNaN()
?
$.isNumeric和之间有什么区别!isNaN()
吗?
I don't see where they will ever return different results.
我不知道他们会在哪里返回不同的结果。
回答by PeeHaa
From the jQuery blog:
来自 jQuery 博客:
Inside jQuery we've found several situations where we need to know if an argument is numeric, or would be successfully converted to a number if it is some other type. We decided to write and document jQuery.isNumeric() since it's a useful utility. Pass it an argument of any type and it returns true or false as appropriate.
在 jQuery 中,我们发现了几种情况,我们需要知道参数是否为数字,或者如果它是其他类型,将成功转换为数字。我们决定编写和记录 jQuery.isNumeric() 因为它是一个有用的实用程序。将任何类型的参数传递给它,它会根据需要返回 true 或 false。
jQuery.isNaN(): This undocumented utility function has been removed. It was confusing because it appropriated the name of a built-in JavaScript function but did not have the same semantics. The new jQuery.isNumeric() serves a similar purpose, but has the benefit of being documented and supported. Despite jQuery.isNaN() being undocumented, several projects on Github were using it. We have contacted them and asked that they use jQuery.isNumeric() or some other solution.
jQuery.isNaN():这个未记录的实用函数已被删除。它令人困惑,因为它占用了一个内置 JavaScript 函数的名称,但没有相同的语义。新的 jQuery.isNumeric() 用于类似的目的,但具有被记录和支持的好处。尽管 jQuery.isNaN() 没有记录,但 Github 上的几个项目都在使用它。我们已经与他们联系并要求他们使用 jQuery.isNumeric() 或其他一些解决方案。
Also see the ticket: http://bugs.jquery.com/ticket/10478
另请参阅票证:http: //bugs.jquery.com/ticket/10478
jQuery's isNumeric()
checks if a value is a number OR can be converted to a number.
jQuery 会isNumeric()
检查值是否为数字或可以转换为数字。
EDIT
编辑
To further clarify what isNan()
does (and what a NaN value is):
进一步阐明是什么isNan()
(以及 NaN 值是什么):
A NaN, which means "Not-a-Number", is classified as a primitive value by the ECMA-262 standard and indicates that the specified value is not a legal number. The function returns true if the argument is not a number and false if the argument is a number.
The classic example of a NaN is zero divided by zero, 0/0
NaN 表示“非数字”,ECMA-262 标准将其归类为原始值,表示指定的值不是合法数字。如果参数不是数字,则函数返回 true,如果参数是数字,则返回 false。
NaN 的经典例子是零除以零,0/0
Code:
document.write(isNaN("Ima String"))
document.write(isNaN(0/0))
document.write(isNaN("348"))
document.write(isNaN(348))
Output:
true
true
false
false
http://www.devguru.com/technologies/ecmascript/quickref/isnan.html
http://www.devguru.com/technologies/ecmascript/quickref/isnan.html
Semi offtopic, but related is this short talk
半离题,但相关的是这个简短的谈话
回答by JJJ
As PeeHaa says, $.isNumeric()
checks whether the value is a number or can be converted to a number, isNaN()
checks strictly only whether the value is NaN
.
正如 PeeHaa 所说,$.isNumeric()
检查该值是否为数字或可以转换为数字,isNaN()
只严格检查该值是否为NaN
.
Here's a handy comparison chart that uses the jQuery documentation'sexamples (comparing $.isNumeric()
to !isNaN
for easier comparison): http://jsfiddle.net/eghE9/
这是一个使用jQuery 文档示例的方便的比较图表(为了更容易的比较$.isNumeric()
而!isNaN
进行比较):http: //jsfiddle.net/eghE9/