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
最新推荐文章于 2024-08-03 17:05:14 发布