Created
April 28, 2015 17:07
-
-
Save sagiavinash/428519259b282e710fe8 to your computer and use it in GitHub Desktop.
Get object class
This file contains hidden or 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