Created
December 10, 2013 21:33
-
-
Save dougalcampbell/7900614 to your computer and use it in GitHub Desktop.
Really get an object's type in JavaScript
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 toType(obj) { | |
var systype = ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1]; | |
if (systype === "Object") { | |
/** | |
* See if this is a custom user type, a la: | |
* function Foo() {} | |
* foo = new Foo(); | |
* toType(foo); // returns "Foo" | |
* toType(Foo); // returns "Function" | |
*/ | |
var usertype = obj.constructor.toString().match(/function (.*?)\(/)[1]; | |
return usertype; | |
} else { | |
return systype; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what about foo.constructor.name?