欢迎访问我的github:,有我的源码解析
常用的判断函数有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 mapjQuery.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 );
这判断也是神判断啊