Skip to content
本页目录

instanceof实现原理

思路

右边变量的原型存在于左边变量的原型链上

js
function myInstanceOf(left, right) {
  let leftValue = left.__proto__
  let rightValue = right.prototype
  while (true) {
    if (leftValue === null) {
      return false
    }
    if (leftValue === rightValue) {
      return true
    }
    leftValue = leftValue.__proto__
  }
}
instanceof实现原理已经加载完毕