Because of the way instanceof
works, you should be able to do
A.prototype instanceof B
But this would only test inheritance, you should have to compare A === B
to test for self-reference:
A === B || A.prototype instanceof B
Babel example:
class A {}
class B extends A {}
class C extends B {}
console.log(C === C) // true
console.log(C.prototype instanceof B) // true
console.log(C.prototype instanceof A) // true
instanceof
is basically implemented as follows:
function instanceof(obj, Constr) {
var proto;
while ((proto = Object.getProtoypeOf(obj)) {
if (proto === Constr.prototype) {
return true;
}
}
return false;
}
It iterates over the prototype chain of the object and checks whether any of the prototypes equals the constructors prototype
property.
So almost like what you were doing, but internally.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…