-
-
Save rwaldron/305890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
var toString = Object.prototype.toString; | |
var class2type = { | |
"[object Boolean]": "boolean", | |
"[object Number]": "number", | |
"[object String]": "string", | |
"[object Function]": "function", | |
"[object Array]": "array", | |
"[object Date]": "date", | |
"[object RegExp]": "regexp" | |
}; | |
// First verion. | |
$.typeA = function( obj ) { | |
if ( !obj ) { | |
return obj === null ? "null" : typeof obj; | |
} | |
return class2type[ toString.call( obj ) ] || "object"; | |
}; | |
// Maybe something faster reducing the need of toString. | |
$.typeB = function( obj ) { | |
var ret = typeof obj; | |
if ( ret !== "object" ) { | |
return ret; | |
} | |
if ( obj === null ) { | |
return "null"; | |
} | |
return class2type[ toString.call( obj ) ] || ret; | |
}; | |
var rbuiltin = /^(?:Boolean|Number|String|Function|Array|Date|RegExp)$/; | |
// For those who prefers regexps instead of maps. | |
// Probably something slower on IE. | |
$.typeC = function( obj ) { | |
var ret = typeof obj; | |
if ( ret !== "object" ) { | |
return ret; | |
} | |
if ( obj === null ) { | |
return "null"; | |
} | |
ret = toString.call( obj ).slice(8, -1); | |
return rbuiltin.test( ret ) ? ret.toLowerCase() : "object"; | |
}; | |
})(jQuery || window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment