13. instanceof 的原理

  1. instanceof 的原理
    问题一:instanceof 的原理是什么?

instanceof可以正确的判断对象的类型,因为内部机制是通过判断对象的原型链中是不是能找到类型的prototype。

我们也可以试着实现一下instanceof

function myInstanceof(left, right) {
let prototype = right.prototype
left = left.proto
while (true) {
if (left === null || left === undefined)
return false
if (prototype === left)
return true
left = left.proto
}
}
以下是对实现的分析:

首先获取类型的原型
然后获得对象的原型
然后一直循环判断对象的原型是否等于类型的原型,直到对象原型为null,因为原型链最终为null

你可能感兴趣的:(13. instanceof 的原理)