Created
December 8, 2009 13:41
-
-
Save mumoshu/251628 to your computer and use it in GitHub Desktop.
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 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