senfish / blog

个人技术博客
4 stars 0 forks source link

2. 实现instanceof关键字 #2

Open senfish opened 3 years ago

senfish commented 3 years ago
function myInstanceOf(left, right) {
  if (left === null || typeof left !== 'object') return false;
  if (typeof right !== 'function') throw new Error('right must be function')
  let L = left.__proto__;
  let R = right.prototype;
  while(L !== null) {
    if (L === R) return true;
    L = L.__proto__;
  }
  return false;
}