Fork me on github

【JS】实现 instanceOf

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
}

 

posted @ 2023-09-10 20:55  zjy4fun  阅读(16)  评论(0编辑  收藏  举报