js基础之手写

42 阅读1分钟

手写Instaceof

语法:target instanceof prototype

注: typeof null 结果是object 但实际null不是对象没有原型对象

function myInstanceof(target,classFunction){
    if(target==null||classFunction==null||target==nudefined||classFunction==undefined){
    return false
    }
    
    while(target.__proto__!=null){
        if(target.__proto__==classFunction.prototype){
            return true
        }else{
            target = target.__proto__
        }
    }
    return false
}