Skip to content

Instantly share code, notes, and snippets.

@ibolmo
Created November 18, 2009 21:13
Show Gist options
  • Select an option

  • Save ibolmo/238253 to your computer and use it in GitHub Desktop.

Select an option

Save ibolmo/238253 to your computer and use it in GitHub Desktop.
istype / instanceof gibberish
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