JavaScript原理——模拟instanceof

new _instanceof(obj, type){
    const baseType = ['string', 'number','boolean','undefined','symbol'];
    //基础类型返回false
    if(baseType.includes(typeof(obj)) || obj === null) { return false };
    let objP = obj.__proto__;
    let typeP = type.prototype;
    //判断obj的原型链是否和type原型相等
    while(true){
        if(objP === null){
            return false
        }
        if(objP === typeP){
            return true
        }
        objP = objP.__proto__
    }
}

猜你喜欢

转载自blog.csdn.net/baidu_38798835/article/details/108567604