Skip to content

Instantly share code, notes, and snippets.

@oyvindkinsey
Created August 8, 2011 14:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyvindkinsey/1131876 to your computer and use it in GitHub Desktop.
Save oyvindkinsey/1131876 to your computer and use it in GitHub Desktop.
// Based on Angus Croll's http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
// Also available at http://jsfiddle.net/7xAqT/
Object.toType = (function() {
var global = this,
toString = Object.prototype.toString;
return function(obj) {
if (obj === global) {
return "Global";
}
var type = typeof obj;
return type == "object" ? toString.call(obj).slice(8, -1) : type;
}
})();
alert([
Object.toType(5),
Object.toType(new Number(5)),
Object.toType(null),
Object.toType(undefined),
Object.toType({}),
Object.toType([])
].join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment