实现 instanceof 操作符

// 实现 instanceof 操作符
function myInstanceof(left, right) {
    // 实现
    if(typeof right!=='function' || right.constructor===null){
        throw new Error('right must be function');
    }
    if(left===null || typeof left!=='object' && typeof left!=='function'){
        return false;
    }
    let leftProto =Object.getPrototypeOf(left);
    while(true){
        if(leftProto===null){
            return false;
        }
        if(leftProto===right.prototype){
            return true;
        }
        leftProto=Object.getPrototypeOf(leftProto);
    }
}

 

posted @ 2025-12-29 14:24  howhy  阅读(10)  评论(0)    收藏  举报