我正在玩instanceof运算符。我想知道我的理解是否正确
var C = function(){};
// in the above statement C has a "prototype" property which points to an object which has
// the constructor property which points to C constructor
var o1 = new C();
// in above statement o1.__proto__ is points to C.prototype. that means inheriting from C.prototype.
console.log(o1 instanceof C) // returns true
//instanceof will check o1.__proto__ is equals to C.prototype(recursively until it finds null object).
C.prototype = {};
console.log(o1 instanceof C) // false;
in the above case o1 was inherited from C.prototype which points to the different object not the present C.prototype object (empty object). so the instanceof condition check fails hence its false.
请告诉我,如果我的解释是错误的
答案 0 :(得分:3)
是的,instanceof检查对象原型链中的构造函数,如果在链中的任何位置找到传递的构造函数,则返回true。因此,如果您销毁函数的原型,就像使用空对象覆盖它一样,那么instanceof将始终返回false。