function MyInstanceof(left, right) {
// 对基本数据类型和null返回false
if ((typeof left !== 'object' && typeof left !== 'function') || left == null) {
return false
}
// 获取原型对象
let proto = Object.getPrototypeOf(left)
let prototype = right.prototype
while (true) {
// 如果原型对象为null则查到尽头了
if (!proto) return false
if (proto == prototype) return true
proto = Object.getPrototypeOf(proto)
}
}
console.log(MyInstanceof('111', String))
console.log(MyInstanceof(new String('1111'), String))
手写instanceof
最新推荐文章于 2025-05-20 08:12:16 发布