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__
      }
}

  

猜你喜欢

转载自www.cnblogs.com/wjgoblin/p/11317978.html