Published 2020-05-19

面试题-手写instanceof

实现 instanceof

核心考察点 getPrototypeOf

function _instanceof(left, right) {
  if (left !== 'object' || left === null) return false
  const proto = Object.getPrototypeOf(left)
  while (true) {
    if (proto === null) return false
    if (proto === right.prototype) return true
    proto = Object.getPrototypeOf(proto)
  }
}
_instanceof(111, Number) //true
_instanceof(new String('11'), String) //true