Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 412 Bytes

手动实现instanceof.md

File metadata and controls

26 lines (20 loc) · 412 Bytes

原理

a instanceof Object

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

实现

    function myInstanceof(target, origin) {
      const proto = target.__proto__;
      if (proto) {
        if (origin.prototype === proto) {
          return true;
        } else {
          return myInstanceof(proto, origin)
        }
      } else {
        return false;
      }
    }