手动实现instanceof

本文最后更新于:2023年3月19日 晚上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function myInstanceOf(obj, fn) {
let proto = Reflect.getPrototypeOf(obj);
while (proto) {
if (proto === fn.prototype) return true;
proto = Reflect.getPrototypeOf(proto);
}
return false;
}

class A {}
class B {}
class AA extends A {
constructor() {
super();
}
}

let a = new AA();

console.log(myInstanceOf(a, Object));

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!