Welcome to my Github:huanshen, have my source code analysis

Common judgment functions include type, isEmptyObject, isFunction, isWindow, isPlainObject, isArraylike, isArray, isNumeric, documentIsHTML, isXML, And its source code is analyzed.

1

type: function( obj ) { if ( obj == null ) { return String( obj ); } // Support: Safari < = 5.1 (functionish RegExp) / / deposit in advance good class2type hash table is used as the accurate judgment of the return typeof obj = = = "object" | | typeof obj = = = "function" ? class2type[ core_toString.call(obj) ] || "object" : typeof obj; },Copy the code

First, it fixes the typeof NULL as object. Secondly, use the pre-stored hash table class2Type for accurate judgment.

The core_toString = obj. ToString; Obj is an object

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

2. Empty isEmptyObject

IsEmptyObject: function(obj) {var name; For (name in obj) {return false; } return true; },Copy the code

3. IsNumeric

//isFinite checks whether the elements of the array are bounded by isNumeric: function(obj) {return! isNaN( parseFloat(obj) ) && isFinite( obj ); },Copy the code

4. Function isFunction

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

Mainly using the previous type.

5, isWindow

IsWindow: function(obj) {return obj! = null && obj === obj.window; },Copy the code

6, isArray

IsArray: array.isarray,Copy the code

Use the isArray that comes with arrays

var arr=[]; console.log(Array.isArray(arr)); //trueCopy the code

7, isPlainObject

// 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; },Copy the code

8 isArraylike.

Function isArraylike(obj) {var length = obj. Length, type = jquery.type (obj); if ( jQuery.isWindow( obj ) ) { return false; } // The element node is also an array of classes if (obj.nodeType === 1 && length) {return true; } return type === "array" || type ! == "function" && ( length === 0 || typeof length === "number" && length > 0 && ( length - 1 ) in obj ); }Copy the code

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; // The root node of XML cannot be the HTML return documentElement. documentElement.nodeName ! == "HTML" : false; };Copy the code

10, documentIsHTML

// Support tests // HTML documentIsHTML =! isXML( doc );Copy the code

That’s god’s judgment, too