function f(_, C) { // instanceof Polyfill
while (_ != null) {
if (_ == C.prototype)
return true;
_ = _.__proto__;
}
return false;
}
Modifying it to check the class directly gives us:
function f(ChildClass, ParentClass) {
_ = ChildClass.prototype;
while (_ != null) {
if (_ == C.prototype)
return true;
_ = _.__proto__;
}
return false;
}
instanceof itself checks if obj.proto is f.prototype, thus:
function A(){};
A.prototype = Array.prototype;
[]instanceof Array // true
and:
function A(){}
_ = new A();
// then change prototype:
A.prototype = [];
/*false:*/ _ instanceof A
// then change back:
A.prototype = _.__proto__
_ instanceof A //true
and:
function A(){}; function B(){};
B.prototype=Object.prototype;
/*true:*/ new A()instanceof B
If it's not equal, proto is swapped with proto of proto in the check, then proto of proto of proto, and so on. Thus:
function A(){}; _ = new A()
_.__proto__.__proto__ = Array.prototype
g instanceof Array //true
and:
function A(){}
A.prototype.__proto__ = Array.prototype
g instanceof Array //true
and:
f=()=>{};
f.prototype=Element.prototype
document.documentElement instanceof f //true
document.documentElement.__proto__.__proto__=[];
document.documentElement instanceof f //false