jQuery 各类判断函数汇总

欢迎访问我的github:huanshen,有我的源码解析

常用的判断函数type,isEmptyObject,isFunction,isWindow,isPlainObject,isArraylike,isArray,isNumeric,documentIsHTML ,isXML,并对其源码进行了解析。

1、类型type

type: function( obj ) {
        if ( obj == null ) {
            return String( obj );
        }
        // Support: Safari <= 5.1 (functionish RegExp)
         利用事先存好的 hash 表 class2type 作精准判断
        return typeof obj === "object" || typeof obj === "function" ?
            class2type[ core_toString.call(obj) ] || "object" :
            typeof obj;
    },

首先其修正了 typeof null 为object的缺陷。其次利用事先存好的 hash 表 class2type 作精准判断。

其中core_toString=obj.toString; obj是一个对象

 Populate the class2type map
jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "),(i,name) {
    class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
var obj={ };arr=[];
    console.log(obj.toString.call(arr));[object Array]
    console.log(obj.toString.call(obj));[object Object]

2、空对象isEmptyObject

3、数字isNumeric
4、函数isFunction
isFunction: return jQuery.type(obj) === "function"5、isWindow
 6、isArray
利用数组自带的isArray来判断

 7、isPlainObject

        if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
            ;
        }

         Support: Firefox <20
         The try/catch suppresses exceptions thrown when attempting to access
         the "constructor" property of certain host objects,ie. |window.location|
         https://bugzilla.mozilla.org/show_bug.cgi?id=814622
        try {
            if ( obj.constructor &&
                    !core_hasOwn.call( obj.constructor.prototype,"isPrototypeOf" ) ) {
                ;
            }
        } catch ( e ) {
             If the function hasn't returned already,we're confident that
         |obj| is a plain object,created by {} or constructed with new Object
        isArraylike

     (such as loading iframes in IE - #4833)
    var documentElement = elem && (elem.ownerDocument || elem).documentElement;
    xml的根节点不可能是HTML
    return documentElement ? documentElement.nodeName !== "HTML" : ;
};

10、documentIsHTML 

 Support tests
    不是xml就是HTML
    documentIsHTML = !isXML( doc );

这判断也是神判断啊

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值
jQuery如何清空form表单数据
jQuery怎么删除元素节点
JQuery怎么循环输出数组元素
jquery怎么实现点击刷新当前页面
怎么用jquery实现文字左右展开收缩效果
jquery怎么删除html属性
如何用jquery实现图片翻转效果
jquery怎么删除样式属性
jquery如何获取当前元素的位置
如何用jquery实现点击展开收缩效果
jquery怎么实现点击隐藏显示效果
jQuery如何获取当前页面url
jQuery怎么获取鼠标的位置坐标
简洁易懂的jQuery:HTML表单与jQuery