打赏

js instanceof 实现原理

function instanceof(left, right) {
    // 获得类型的原型
    let prototype = right.prototype
    // 获得对象的原型
    left = left.__proto__
    // 判断对象的类型是否等于类型的原型
    while (true) {
        if (left === null)
            return false
        if (prototype === left)
            return true
        left = left.__proto__
    }
}

//原理:对象的原型链中是不是能找到类型的 prototype
posted @ 2019-03-09 20:16  孟繁贵  阅读(576)  评论(0)    收藏  举报
TOP