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