jQuery改变了isNumeric(最新版本)? [英] jQuery's change of isNumeric (in latest versions)?

查看:72
本文介绍了jQuery改变了isNumeric(最新版本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近版本,jQuery用于检查数字是否通过:

Until recent version , jQuery used to check if numeric via :

return!isNaN(parseFloat(obj))&& isFinite(obj);

第一个部分用于:


  • parseFloat(d)// Nan

  • !isNaN(parseFloat(Infinity))// true但不是数字

  • parseFloat("d") //Nan
  • !isNaN( parseFloat(Infinity)) //true but not a number

第二个部分用于:


  • isFinite('2')// true

  • isFinite('2') //true

但是在最近的版本中,他们更改了它并将其更改为:

But in recent version they changed it and changed it to :

return !jQuery.isArray(obj) && (obj - parseFloat(obj) + 1) >= 0;

问题:

在之前的版本中不够好,他们将其更改为新版本?为什么他们检查数组?

What was not good enough in the previous version that they changed it to the new one ? And why do they check if array?

推荐答案

obj的相同值回答你的问题: [3]

!isNaN(parseFloat(obj) )&& isFinite(obj) true for [3]

(obj - parseFloat(obj)+ 1)> = 0 true for [3]

这些问题背后的原因是转换为字符串或数字发生在< a href =http://es5.github.io/#x15.1.2.3 =noreferrer> parseFloat isFinite 并且转换为字符串数组是用逗号连接其元素到字符串的转换结果。

The reason behind those problems is that a conversion to string or number occurs in parseFloat and in isFinite and that the conversion to string of an array is the result of joining with commas the conversion of its elements to strings.

所以这个改变是一个bug修复。

So this change is a bug fix.

请注意,您仍然可以使用 {toString:function(){return 3}} 等值使其失败,但不清楚jQuery应该返回什么在这种情况下(这个对象真的希望显示为数字,毕竟)。

Note that you can still make it "fail" with values like {toString:function(){ return 3}} but it's unclear what jQuery should really return in such a case (this object really wants to appear as a number, after all).

这篇关于jQuery改变了isNumeric(最新版本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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