模拟instanceof实现

285 阅读1分钟

/**
* 模拟instanceof
*/
const simulateInstanceof = (L, R) => {
    // L表示左边表达式,R表示右边表达式
    const O = R.prototype; // 取R的显示原型
    L = L.__proto__; // 取L的隐式原型
    while (true) {
        if (L === null) {
            return false;
        }
        if (O === L) {
            return true;
        }
        L = L.__proto__;
    }
}

参考资料

  1. www.cxymsg.com/guide/jsWri…

微信公众号“前端那些事儿”