实现instanceof关键字

161 阅读1分钟

实现instanceof关键字

instanceof就是判断构造函数的prototype属性是否出现在实例的原型链上

function instanceofs(left,right){
  let proto=left.__proto__
  while(true){
    if(proto===null)return false
    if(proto===right.prototype) return true
    proto=proto.__proto__
  }
}
console.log( instanceofs([1,2,3],String))