instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
A instanceof B
检查A.__proto__是否在B.prototype
function myInstanceof(A, B) {
A = A.__proto__;
B = B.prototype;
while (A) {
if (A === B) {
return true;
}
A = A.__proto__;
}
return false;
}
function C() {
}
function D() {
}
var o = new C();
console.log(myInstanceof(o, C));//true
console.log(myInstanceof(o, D));//false
console.log(myInstanceof(o, Object));//true
console.log(myInstanceof(C.prototype, Object));//true