instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。
语法: object instanceof constructor object要检测的对象.constructor某个构造函数

实现 instanceof

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__
    }
}

参考资料:
前端进阶之道

相关文章:

  • 2022-01-01
  • 2021-06-19
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-23
猜你喜欢
  • 2021-07-23
  • 2022-12-23
  • 2021-11-27
  • 2021-11-26
  • 2022-12-23
  • 2021-09-15
  • 2021-12-27
相关资源
相似解决方案