js实现instanceof

Posted wjgoblin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现instanceof相关的知识,希望对你有一定的参考价值。

    instanceof 是通过原型链判断的,A instanceof B, 在A的原型链中层层查找,是否有原型等于B.prototype,如果一直找到A的原型链的顶端null,仍然不等于B.prototype,那么返回false,否则返回true.
   
function instance(left,right)
      left=left.__proto__
      right=right.prototype
      while(true)
           if(left==null)
                return false;
           if(left===right)
                return true;
           left=left.__proto__
      

  

 

以上是关于js实现instanceof的主要内容,如果未能解决你的问题,请参考以下文章