美文网首页
手写instanceof

手写instanceof

作者: Ag_fronted | 来源:发表于2021-07-08 11:37 被阅读0次
function Animal(name) {
  this.name = name;
}

function myInstanceof(a, b) {
  let L = a;
  while (true) {
    if (L.__proto__ === null) {
      return false;
    }
    if (L.__proto__ === b.prototype) {
      return true;
    }
    L = L.__proto__;
  }
}

const dog = new Animal("dog");
console.log(myInstanceof(dog, Array));
console.log(myInstanceof(dog, Animal));
console.log(myInstanceof(dog, Object));

相关文章

网友评论

      本文标题:手写instanceof

      本文链接:https://www.haomeiwen.com/subject/giozultx.html