gpt4 book ai didi

javascript - $.isNumeric 与 isNaN

转载 作者:行者123 更新时间:2023-12-03 21:39:01 25 4
gpt4 key购买 nike

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

我不知道他们会在哪里返回不同的结果。

最佳答案

来自 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.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() 检查某个值是否是数字或是否可以转换为数字。

编辑

为了进一步阐明 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

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

半离题,但相关的是 this short talk

关于javascript - $.isNumeric 与 isNaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8012807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com