如何实现instanceof

instanceof 运算符用于测试构造函数的 prototype 属性是否出现在对象原型链中的任何位置

function instance_of(obj, Obj) {
   obj = obj.__proto__;
   let Obj_Prototype = Obj.prototype;
   while (true) {
      if (obj === null) return false;
      if (obj === Obj_Prototype) {
         return true;
      }
      obj = obj.__proto__;
   }
}

 

posted @ 2021-02-23 16:52  程序員劝退师  阅读(54)  评论(0编辑  收藏  举报