<script>
//手写js实现instanceof效果
function Instanceof(left, right) {
//获取到对象的原型
let proto = Object.getPrototypeOf(left)
//获取到右侧的protoype对象
let prototype = right.prototype
while (true) {
//如果不在对象原型上,返回false
if (!proto) return false
//如果左右两侧都在类型一样,返回true
if (proto === prototype) return true
//没有找到,继续往上找,直到null为止
proto = Object.getPrototypeOf(proto)
}
}
console.log(Instanceof([], Array));//true
console.log(Instanceof([], Object));//true
console.log(Instanceof(1, Number));//true
</script>
手写js实现instanceof效果
猜你喜欢
转载自blog.csdn.net/qq_72760247/article/details/128770379
今日推荐
周排行