Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Really get an object's type in JavaScript

View toType.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
Something went wrong with that request. Please try again.