JZTXT
  • 首页
  • Ai
  • Java
  • Python
  • Android
  • Mysql
  • JavaScript
  • Html
  • CSS

【JS】实现 instanceOf

发布时间 2023-09-10 21:04:07作者: zjy4fun

https://github.com/zjy4fun/notes/tree/main/demos/js-instanceof

原型就是一个对象,instanceof 就是检查构造函数的原型是否在对象的原型链上

 

function myInstanceOf(obj, constructorFn) {
    const prototype = constructorFn.prototype
    while(obj !== null) {
        if(obj.__proto__ === prototype) {
            return true
        }
        obj = obj.__proto__
    }
    return false
}

 

    本栏目推荐文章
  • JavaScript——instanceof操作符的原理
  • instanceof原理
  • JavaScript中的instanceof运算符
  • 秦疆的Java课程笔记:72 面向对象 instanceof和类型转换
  • Error in nextTick: “TypeError: Right-hand side of ‘instanceof‘ is not callable“报错解决
  • instanceof关键字
  • typeof 主要用于检测基本数据类型,对于引用类型不大适用(因为所有引用类型的值都是 Object 的实例),只能知道是对象,而具体是什么类型的对象却无法判断。这时需要 instanceof 来判断到底是什么类型(普通Object类型、Array类型、Function类型、RegExp类型、Date类型,基本包装类型【包括:Boolean、Number、String类型】)的对象
  • instanceof 在跨 frame 对象构建的场景下会失效
  • 关于向上转型以及向下转型、instanceof的一些应用。
  • JAVA - instanceof 模式匹配
版权声明:本网站为非赢利性站点,本网站所有内容均来源于互联网相关站点自动搜索采集信息,相关链接已经注明来源。
联系我们