Skip to content

Latest commit

 

History

History
32 lines (28 loc) · 565 Bytes

instanceof.md

File metadata and controls

32 lines (28 loc) · 565 Bytes

原理

a instanceof Object

判断Object的prototype是否在a的原型链上。

实现

function myInstance(target, origin) {
    let proto = target.__proto__
    if (proto) {
        if (proto === origin.prototype) {
            return true
        } else {
            return myInstance(proto, origin)
        }
    } else {
        return false
    }
}