$ .isNumeric vs. isNaN [英] $.isNumeric vs. isNaN

查看:104
本文介绍了$ .isNumeric vs. isNaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ .isNumeric 之间有什么区别!isNaN()

我看不出它们会在哪里返回不同的结果。

I don't see where they will ever return different results.

推荐答案

来自jQuery博客:

From the jQuery blog:


在jQuery中我们发现了几种我们需要知道的情况参数是数字,或者如果是其他类型,则会成功转换为数字。我们决定编写并记录jQuery.isNumeric(),因为它是一个有用的实用程序。传递任何类型的参数,并在适当时返回true或false。

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.isNaN():这个未记录的实用程序函数已被删除。它令人困惑,因为它占用了内置JavaScript函数的名称,但没有相同的语义。新的jQuery.isNumeric()用于类似的目的,但具有记录和支持的好处。尽管jQuery.isNaN()没有记录,但Github上的几个项目正在使用它。我们已联系过他们并要求他们使用jQuery.isNumeric()或其他解决方案。

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.

另请参阅票证: http://bugs.jquery.com/ticket/10478

jQuery的 isNumeric()检查值是否为数字或者可以转换为数字。

jQuery's isNumeric() checks if a value is a number OR can be converted to a number.

编辑

进一步说明 isNan()确实(以及NaN值是多少):

To further clarify what isNan() does (and what a NaN value is):


一个NaN,意思是非数字,按ECMA-262标准分类为原始值,表示指定值不是合法数字。如果参数不是数字,则函数返回true;如果参数是数字,则返回false。

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.

NaN的经典例子是零除零,0/0

The classic example of a NaN is zero divided by zero, 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

半offtopic,但相关的是这个简短的谈话

Semi offtopic, but related is this short talk

这篇关于$ .isNumeric vs. isNaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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