instanceof原理

instanceOf用来判断右边的prototype是否在左边的原型链上,告诉我们左边是否是右边的实例。

function instanceof(left, right) {
// 获得类型的原型
let prototype = right.prototype
// 获得对象的原型
left = left.proto
// 判断对象的类型是否等于类型的原型
while (true) {
if (left === null){
return false
}
if (prototype === left){
return true
}
left = left.proto
}
}

原文地址:https://www.cnblogs.com/Fairy-Tail-blogs/p/14943077.html