欢迎访问我的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(" "), function(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

// 检查对象是否为空(不包含任何属性)
isEmptyObject: function( obj ) {
var name;
//对于空对象是不会执行for循环语句的
for ( name in obj ) {
return false;
}
return true;
},

3、数字isNumeric

// 确定它的参数是否是一个数字
//isFinite判断数组的元素是否是有界的
isNumeric: function( obj ) {
return !isNaN( parseFloat(obj) ) && isFinite( obj );
},

4、函数isFunction

isFunction: function( obj ) {
return jQuery.type(obj) === "function";
},

主要是利用前面的type.

5、isWindow

// 判断传入对象是否为 window 对象
isWindow: function( obj ) {
return obj != null && obj === obj.window;
},

6、isArray

// 判断传入对象是否为数组
isArray: Array.isArray,

利用数组自带的isArray来判断

var arr=[];
console.log(Array.isArray(arr));//true

7、isPlainObject

// 测试对象是否是纯粹的对象
// 通过 "{}" 或者 "new Object" 创建的
isPlainObject: function( obj ) {
// Not plain objects:
// - Any object or value whose internal [[Class]] property is not "[object Object]"
// - DOM nodes
// - window
// Make sure that DOM nodes and window objects don't pass through, as well
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
} // 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" ) ) {
return false;
}
} catch ( e ) {
return false;
} // If the function hasn't returned already, we're confident that
// |obj| is a plain object, created by {} or constructed with new Object
return true;
},

 8、isArraylike

//判断是否是数组,类数组,带length的json,是的话就返回真
function isArraylike( obj ) {
var length = obj.length,
type = jQuery.type( obj ); if ( jQuery.isWindow( obj ) ) {
return false;
}
//元素节点也是类数组
if ( obj.nodeType === 1 && length ) {
return true;
} return type === "array" || type !== "function" &&
( length === 0 ||
typeof length === "number" && length > 0 && ( length - 1 ) in obj );
}

9、isXML

/**
* Detect xml
* @param {Element|Object} elem An element or a document
*/
isXML = Sizzle.isXML = function( elem ) {
// documentElement is verified for cases where it doesn't yet exist
// (such as loading iframes in IE - #4833)
var documentElement = elem && (elem.ownerDocument || elem).documentElement;
//xml的根节点不可能是HTML
return documentElement ? documentElement.nodeName !== "HTML" : false;
};

10、documentIsHTML

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

这判断也是神判断啊

jQuery 各类判断函数汇总的更多相关文章

  1. jQuery中的函数汇总1

    欢迎访问我的github:huanshen,有我的源码解析 1.each 跟for循环很像,但是更有用,如果你理解了就知道了. // 遍历一个数组或者对象 // obj 是需要遍历的数组或者对象 // ...

  2. jQuery如何判断元素是否是隐藏的?

    jQuery函数简介: is(expr) 用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true. 如果没有元素符合,或者表达式无效,都返回'false'. 注 ...

  3. MySQL1:MySQL函数汇总

    前言 MySQL提供了众多功能强大.方便易用的函数,使用这些函数,可以极大地提高用户对于数据库的管理效率,从而更加灵活地满足不同用户的需求.本文将MySQL的函数分类并汇总,以便以后用到的时候可以随时 ...

  4. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...

  5. jQuery1.11源码分析(9)-----初始化jQuery对象的函数和关联节点获取函数

    这篇也没什么好说的,初始化jQuery对象的函数要处理多种情况,已经被寒冬吐槽烂了.关联节点获取函数主要基于两个工具函数dir和sibling,前者基于指定的方向遍历,后者则遍历兄弟节点(真的不能合并 ...

  6. MySQL函数汇总

    前言 MySQL提供了众多功能强大.方便易用的函数,使用这些函数,可以极大地提高用户对于数据库的管理效率,从而更加灵活地满足不同用户的需求.本文将MySQL的函数分类并汇总,以便以后用到的时候可以随时 ...

  7. 详细解读Jquery各Ajax函数

    $.get(),$.post(),$.ajax(),$.getJSON() 一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表,callba ...

  8. C/C++常用头文件及函数汇总

    转自: C/C++常用头文件及函数汇总 C/C++头文件一览 C #include <assert.h> //设定插入点#include <ctype.h> //字符处理#in ...

  9. jquery的匿名函数研究

    jQuery片段: ? 1 2 3 ( function (){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他人一样很兴奋地想看看源码是什么样的.然而,在 ...

随机推荐

  1. 《principal component analysis based cataract grading and classification》学习笔记

    Abstract A cataract is lens opacification caused by protein denaturation which leads to a decrease i ...

  2. CxGrid鼠标移到更改颜色

    CxGrid鼠标移到更改颜色 设置表单中TcxGrid1DBTableView的Styles属性,设置Selection procedure TForm1.cxGrid1DBTableView1Mou ...

  3. asp.net core sdk & runtime 镜像[已更新至2.2.0]

    在官方镜像的脚本上, 增加了System.Drawing相关的依赖库 以北京时间为默认的时间 2.2.0 Windows SDK地址: 官方: https://dotnetcli.blob.core. ...

  4. mysql 多个and的简写

    select * from test where name='zj' and sex='2'; 我以前也经常用这种写法,今天爬出去看了一下某位人写的,用了一下也挺好用的 下面这种写法,一一对应关系

  5. Android TV 开发(5)

    本文来自网易云社区 作者:孙有军 问题3:TV launcher中没有入口图标 如果需要出现入口图标,你必须要在AndroidManifest中配置action为android.intent.acti ...

  6. Spring Cloud实践之服务注册与发现Eureka

    一.简述: 服务提供者producer与服务消费者consumer都注册到eureka server,然后服务consumer在其应用内直接调用producer的服务名来调用服务,而不是像之前一样调用 ...

  7. IOS渗透测试第一步-基础知识统一放送

    原文: http://www.websecgeeks.com/2017/04/ios-application-pentesting-part-3.html http://www.websecgeeks ...

  8. Mysql数据优化--DBA梳理珍藏篇

    1. 优化SQL 1)     通过show status了解各种sql的执行频率 show status like 'Com_%'        了解 Com_select,Com_insert 的 ...

  9. PCA (主成分分析)详解 (写给初学者) 结合matlab(转载)

    一.简介 PCA(Principal Components Analysis)即主成分分析,是图像处理中经常用到的降维方法,大家知道,我们在处理有关数字图像处理方面的问题时,比如经常用的图像的查询问题 ...

  10. (转) lsof 一切皆文件

    原文:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/lsof.html lsof(list open files)是一个查看当前系统文 ...