Home  >  Article  >  Web Front-end  >  jQuery1.6 type judgment implementation code_jquery

jQuery1.6 type judgment implementation code_jquery

WBOY
WBOYOriginal
2016-05-16 18:02:441022browse

First define
global variable class2type = {};
rdigit = /d/, // Regularly determine whether it is a number
toString = Object.prototype.toString;
Define class2type through jQuery.each Attributes and values:

Copy code The code is as follows:

jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
class2type[ "[object " name "]" ] = name.toLowerCase();
});
Type: function( obj ) {
return obj == null ?String( obj ) :class2type[ toString.call(obj) ] || "object";//Everything in js is an object, so you can use toString .call(obj) returns the corresponding object type
// If you use this method directly to determine the type, document.getElementById under IE is considered to be object, and other browsers consider it to be function
// If the value passed in If it is NaN, "number" will be returned
 }
isFunction: function( obj ) {return jQuery.type(obj) === "function";}//Under IE, if document.getElementById is passed in , the return is false, IE is object
isArray: Array.isArray || function( obj ) {return jQuery.type(obj) === "array";}//If the native Array has the isArray method Use Array.isArray, otherwise customize this method
isWindow: function( obj ) {return obj && typeof obj === "object" && "setInterval" in obj;}//Determine whether it is a window object
isNaN : function( obj ) {return obj == null || !rdigit.test( obj ) || isNaN( obj );}//
 isEmptyObject: function( obj ) {
for ( var name in obj ) {//The basis for judgment is whether the object has attributes or methods
return false;
}
return true;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn