# 解题思路 or 实现原理

判断实例对象的__proto__和构造函数的prototype是不是引用同一个地址

instanceof

prototype

# 实现代码

/*
 * @Author: Rainy
 * @Date: 2019-11-14 19:03:03
 * @LastEditors: Rainy
 * @LastEditTime: 2019-11-15 13:48:20
 */

export function _instanceof(left: any, right: any): boolean {
  const rt = right.prototype;
  let lt = left.__proto__;
  while (true) {
    if (lt === null) {
      return false;
    }
    if (lt === rt) {
      return true;
    }
    lt = lt.__proto__;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20