Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created December 8, 2009 13:41
Show Gist options
  • Save mumoshu/251628 to your computer and use it in GitHub Desktop.
Save mumoshu/251628 to your computer and use it in GitHub Desktop.
function getClass(o) {
var res;
return o == null
? o === null ? "null" : "undefined"
: (res = o.constructor
.toString()
.match(/^function ([^\(]+)|object (\w+)/))
&& res[1] || res[2];
}
function assertEquals(a,b) {
a === b
? console.log(a,"and", b, "are equal.")
: console.error(a, "and", b, " are not equal.");
}
assertEquals(getClass(1), "Number");
assertEquals(getClass(""), "String");
assertEquals(getClass(new (function MyClass(){})()), "MyClass");
assertEquals(getClass(document.createElement('div')), "HTMLDivElement");
assertEquals(getClass(document.createElement('span')), "HTMLSpanElement");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment