首页 > 技术文章 > js instanceof 实现原理

mengfangui 2019-03-09 20:16 原文

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

推荐阅读