只需补充@apsillers 的答案
object instanceof constructor
var superProto = {}
// subProto.__proto__.__proto__ === superProto
var subProto = Object.create(superProto);
subProto.someProp = 5;
// sub.__proto__.__proto__ === subProto
var sub = Object.create(subProto);
console.log(superProto.isPrototypeOf(sub)); // true
console.log(sub instanceof superProto); // TypeError: Right-hand side of 'instanceof' is not callable
// helper utility to see if `o1` is
// related to (delegates to) `o2`
function isRelatedTo(o1, o2) {
function F(){}
F.prototype = o2;
// ensure the right-hand side of 'instanceof' is callable
return o1 instanceof F;
}
isRelatedTo( b, a );
TypeError:'instanceof' 的右侧不可调用
instanceof需要右边的值是可调用的,这意味着它必须是一个函数(MDN 调用它作为构造函数)
并instanceof测试constructor.prototype对象原型链中的存在。
但isPrototypeOf()没有这样的限制。同时instanceof检查superProto.prototype,直接isPrototypeOf()检查superProto。