Created
June 27, 2012 14:32
-
-
Save adamrobbie/3004432 to your computer and use it in GitHub Desktop.
Utility method to get class of any JS object
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 getClass(obj) { | |
if (typeof obj === "undefined") | |
return "undefined"; | |
if (obj === null) | |
return "null"; | |
return Object.prototype.toString.call(obj) | |
.match(/^\[object\s(.*)\]$/)[1]; | |
} | |
getClass("") === "String"; | |
getClass(true) === "Boolean"; | |
getClass(0) === "Number"; | |
getClass([]) === "Array"; | |
getClass({}) === "Object"; | |
getClass(null) === "null"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment