Created
November 18, 2009 21:13
-
-
Save ibolmo/238253 to your computer and use it in GitHub Desktop.
istype / instanceof gibberish
This file contains hidden or 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 log(var_) { | |
| console.log(var_); | |
| return var_; | |
| } | |
| function istype(test, Ctor) { | |
| if (Ctor == void 0 && test == void 0) return true; | |
| return log(test.constructor == Ctor || test instanceof Ctor); | |
| } | |
| istype('string', String); | |
| istype(function(){}, Function); | |
| istype(10, Number); | |
| istype(false, Boolean); | |
| istype(/regexp/, RegExp); | |
| istype(new Date(), Date); | |
| istype(document.createElement('div'), Element); | |
| istype(undefined, null); | |
| istype({}, Object); | |
| var A = new Class({}); | |
| var b = new A(); | |
| istype(A, Class); | |
| istype(b, A); | |
| var C = new Class({ Extends: A }); | |
| istype(C, Class); | |
| var d = new C(); | |
| istype(d, C); | |
| istype(d, A); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment