Skip to content

Instantly share code, notes, and snippets.

@dougalcampbell
Created December 10, 2013 21:33
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dougalcampbell/7900614 to your computer and use it in GitHub Desktop.
Really get an object's type in JavaScript
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;
}
}
@theohogberg
Copy link

what about foo.constructor.name?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment