function myInstanceof(obj, constructor) {
// 1. 排除基本类型和非对象
if (typeof obj !== 'object' || obj === null) return false;
// 2. 获取对象的原型
let proto = Object.getPrototypeOf(obj);
// 3. 遍历原型链,对比 constructor.prototype
while (proto !== null) {
if (proto === constructor.prototype) {
return true;
}
proto = Object.getPrototypeOf(proto);
}
return false;
}
const n = new Number(1)
console.log(myInstanceof(n, Number))