Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from rkatic/README.md
Created February 16, 2010 20:36
Show Gist options
  • Save rwaldron/305890 to your computer and use it in GitHub Desktop.
Save rwaldron/305890 to your computer and use it in GitHub Desktop.
(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