用法
function Person(name, age) {
this.name = name
this.age = age
this.say = function () {
console.log('my name is' + this.name)
}
}
a = new Person('张三', 18)
a instanceof Person // true
a instanceof Function // false
a instanceof Object // true
实现
寻找left的对象中的原型链中是否存在right函数的原型对象(right.prototype)
function myInstanceof(left, right) {
prototype = right.prototype
while(true) {
if (left.__proto__ === prototype) {
return true
} else if (left.__proto__ !== null) {
left = left.__proto__
} else {
return false
}
}
}