instanceof的实现原理

115 阅读1分钟

如果A沿着原型链能找到B的原型对象(B.prototype),那么A instanceof B为true

const instanceOf=(A,B)=>{
    //遍历A的原型链
    let p=A
    while(p){
        if(p===B.prototype){
            return true
        }
        p=p.__proto__
    }
    return false
}