js实现instanceof

关于instanceof的具体用法和原理,可参考:https://blog.csdn.net/LL18781132750/article/details/81115081

下面给出instanceof的实现代码:

function myInstanceof(left,right){
    var proto = left.__proto__;
    var protoType = right.prototype;
    while(true){
        if(proto === null){
            return false
        }
        if(proto == protoType){
            return true
        }
        proto = proto.__proto__
    }
}

 

posted @ 2020-04-15 10:11  想学JS的前端  阅读(426)  评论(0编辑  收藏  举报