跳到主要内容

instanceof

function fakeInstanceof(instance, constructor) {
try {
const proto = Reflect.getPrototypeOf(instance)
// const proto = Object.getPrototypeOf(instance)
if (proto === constructor.prototype) {
return true
}
return fakeInstanceof(proto, constructor)
} catch {
return false
}
}

测试代码

class Parent {}
class Child extends Parent {}
const child = new Child()

console.log(fakeInstanceof(child, Child))
console.log(fakeInstanceof(child, Parent))
console.log(fakeInstanceof(child, Object))
console.log(fakeInstanceof(1, Number)) // false
console.log(fakeInstanceof(null, Object))
console.log(fakeInstanceof(() => {}, Function))