Created
August 8, 2011 14:41
-
-
Save oyvindkinsey/1131876 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
// 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