jQuery的isNumeric疑惑

贴上jQuery的源码

    isNumeric: function( obj ) {

        // As of jQuery 3.0, isNumeric is limited to
        // strings and numbers (primitives or objects)
        // that can be coerced to finite numbers (gh-2662)
        var type = jQuery.type( obj );
        return ( type === "number" || type === "string" ) &&

            // parseFloat NaNs numeric-cast false positives ("")
            // ...but misinterprets leading-number strings, particularly hex literals ("0x...")
            // subtraction forces infinities to NaN
            !isNaN( obj - parseFloat( obj ) );
    },

为什么需要obj - parseFloat( obj )而不是直接obj - obj
是为了排除什么情况?

在问题补充回答。
空字符串的情况

阅读 2k
2 个回答
 // ...but misinterprets leading-number strings, particularly hex literals ("0x...")

注释里已经写的很明白了啊

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题