手写 instanceof

91 阅读1分钟
    function myInstanceof(left, right) {
      let prototype = right.prototype;
      console.log(prototype)
      left = left.__proto__;
      console.log(left)
      while (true) {
        if (left === null || left === undefined)
          return false
        if (prototype === left)
          return true
        left = left.__proto__
        console.log(left)
      }
    }
    function Car(make, model, year) {
      this.make = make;
      this.model = model;
      this.year = year;
    }
    const auto = new Car('Honda', 'Accord', 1998);

    console.log(myInstanceof(auto, Object));