Functionality equivalent to the instanceof
operator could be implemented in plain Javascript like this:
function instance_of(obj, constructor) {
var o = obj, prot=constructor.prototype;
while(o != null && o !== Object) {
o = Object.getPrototypeOf(o);
if(o === prot) return true;
}
return false;
}
That uses the ES5 function, Object.getPrototypeOf()
to access the prototype of a given object instance. getPrototypeOf
is supported in all modern browsers, and javascript environments. Most older browsers relied on the __proto__
property (now deprecated/discouraged) to provide access to the same information.