2020-12-06 instanceof 的原理,并代码实现

解答 :

遍历A的原型链,如果能找到B的prototype,返回true  找不到返回false


const instanceOf = (A, B) => {
    let p = A;
    while (p) {
        if (p === B.prototype) {
            return true
        }
        p = p.__proto__;
    }
    return false
}

猜你喜欢

转载自blog.csdn.net/wanghongpu9305/article/details/110772588