1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// instanceof 的原理
function myInstanceOf(left, right) {
let prototype = right.prototype
left = left.__proto__
while (true) {
if (left == null || left == undefined) return false
if (left === prototype) return true
left = left.__proto__
}
}
/* 以下是对实现的分析:
- ⾸先获取类型的原型
- 然后获得对象的原型
- 然后⼀直循环判断对象的原型是否等于类型的原型,直到对象原型为 null,因为原型链最终为 null */

function myInstanceof(left, right) {
    if (typeof left !== 'object' || right === null) {
        return false
    }
    right = right.prototype
    left = left__proto__
    while (true) {
        if (left === null) return false
        if (left === right) return true
        left = left.left__proto__
    }
}