instanceof 实现

A instanceof B 

// 实现 instanceof

function instance(a, b) {
  const proto = a.__proto__; // eslint-disable-line
  if (proto) {
    if (proto === b.prototype) {
      return true;
    } else {
      return instance(proto, b);
    }
  } else {
    return false;
  }
}

class Base {}

class A extends Base {}
class B extends Base {}
const a = new A();

console.log(a instanceof Base);
console.log(instance(a, B));

 

posted @ 2019-05-16 18:36  shangyueyue  阅读(329)  评论(0编辑  收藏  举报