Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
2.02k stars 235 forks source link

instanceof #32

Open Sunny-117 opened 2 years ago

Sunny-117 commented 2 years ago
function myInstanceof(left, right) {
  let proto = Object.getPrototypeOf(left), // 获取对象的原型
      prototype = right.prototype; // 获取构造函数的 prototype 对象

  // 判断构造函数的 prototype 对象是否在对象的原型链上
  while (true) {
    if (!proto) return false;
    if (proto === prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true
faga295 commented 2 years ago
function myInstanceof(left, right) {
  if (!left) return false;
  return (
    left.__proto__ === right.prototype || myInstanceof(left.__proto__, right)
  );
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true
23olddog commented 1 year ago
function myInstanceof(left = null, right) {
    // 对于左侧参数如果是非对象直接返回false
    if (Object(left) !== left) return false
    // 对于右侧参数可以认为只能为函数且不能没有Prototype属性
    if (typeof right !== 'function' || !right.prototype) throw new TypeError('Right-hand side of 'instanceof' is not an object')
    // 声明一个变量获取对象的__proto__
    let link = left.__proto__
    // 做循环(当link最终指向null,如果指向null的情况下都找不到那就返回false)
    while (link !== null) {
        // 如果找到说明R.prototype在L的原型链上,即返回true
        if(link === right.prototype) return true
        // 逐级向下
        link = link.__proto__
    }
    return false
}
//编写测试用例
function Person(name, age) {
    this.name = name;
    this.age = age;
  }
  function School(name, address) {
    this.name= name;
    this.address = address;
  }
  const p1 = new Person('james', 23);
  console.log(myInstanceof(p1, Person) + '--' + myInstanceof(p1, Object) + '--' + myInstanceof(p1, School))
veneno-o commented 1 year ago
function _instanceof(obj, cla) {
    let objProto = Object.getPrototypeOf(obj);
    const claProto = cla.prototype;

    while (true) {
        if (objProto === null) {
            return false;
        }
        if (objProto === claProto) {
            return true;
        }
        objProto = Object.getPrototypeOf(objProto);
    }
}
ntting-top commented 1 year ago
<script>
        function _instanceOf(left, right) {
            const rightProto = right.prototype;
            left = left.__proto__
            while(left.__proto__ !== null) {
                if(rightProto === left) {
                    return true;
                }
                left = left.__proto__;
            }
            return false
        }
        // 测试用例
        function Person () {}
        function Person2 () {}
        const no = new Person()
        const no2 = new Person2()
        console.log(_instanceOf(no2, Person))
        console.log(_instanceOf(no, Person))
    </script>
coffeeAndTeaa commented 1 year ago
function myInstanceof(left, right) {
  if (!left) return false;
  return (
    left.__proto__ === right.prototype || myInstanceof(left.__proto__, right)
  );
}
function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object));
// console.log(p instanceof Person);//true

this is super elegant

bearki99 commented 1 year ago
function myInstance(obj, proto) {
  let proto1 = Object.getPrototypeOf(obj);
  let proto2 = proto.prototype;
  while (1) {
    if (proto1 == null) return false;
    if (proto1 == proto2) return true;
    proto1 = Object.getPrototypeOf(proto1);
  }
}

注:尽量使用Object.getPrototypeOf获取对象的原型 image

sv-98-maxin commented 1 year ago
function instanceOf(left, right) {
  let a = Object.getPrototypeOf(left), b = right.prototype;
  while (a) {
    if (a === b) {
      return true
    }
    a = Object.getPrototypeOf(a)
  }
  return false
}
kangkang123269 commented 1 year ago
function myInstanceof(obj, constructor) {
  let proto = Object.getPrototypeOf(obj);
  while (proto) {
    if (proto === constructor.prototype) {
      return true;
    }
    proto = Object.getPrototypeOf(proto);
  }
  return false;
}
LifeIsTerrible commented 1 year ago
function _instanceof(left, right) {
    let proto = Object.getPrototypeOf(left),
        prototype = right.prototype;
    while (true) {
        if (!proto) return false;
        if (proto === prototype) return true;
        // 查找原型链
        Object.getPrototypeOf(proto);
    }
}
Digoler commented 1 year ago
 const myInstanceof = (left, right) => {
     let proto = Object.getPrototypeOf(left)
     while (true) {
         if (proto === null) return false
         if (proto === right.prototype) return true
         proto = Object.getPrototypeOf(proto)
        }
    }
YMnotafraid commented 1 year ago
function myinstanceof(left, right) {
  let lpro = left.__proto__; //Object.getPrototypeOf(left)
  let rpro = right.prototype;
  while (lpro) {
    if (lpro === rpro) return true;
    lpro = lpro.__proto__;
  }
  return false;
}

console.log(myinstanceof(Function, Function)); //true
Bbbtt04 commented 1 year ago
console.log([] instanceof Array);

function myInstanceof(left, right) {
  let protoLeft = Object.getPrototypeOf(left)
  let protoRight = right.prototype

  while (true) {
    if (protoLeft === null) return false
    if (protoLeft === protoRight) return true
    protoLeft = Object.getPrototypeOf(left)
  }
}
console.log(myInstanceof([], Array));
xun-zi commented 1 year ago
function myInstanceof(left, right) {
        let proto = left.__proto__;
        const prototypee = right.prototype;
        while (true) {
            if (!proto) return false;
            if (proto == prototypee) return true;
            proto = proto.__proto__;
        }
    }
    function Person() { };
    var p = new Person();
    console.log(myInstanceof(p, Object));
cscty commented 1 year ago
function instanceOf(son, father) {
        if (Object.getPrototypeOf(son) === null) return false
        if (father.prototype === Object.getPrototypeOf(son)) return true
        else return instanceOf(Object.getPrototypeOf(son), father)
      }
luckymore commented 1 year ago

都遗漏了左侧必须是个 object 类型 1 instanceof Number === false

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object.

function myInstanceof(left, right) {
  if (typeof left !== 'object') return false
  let up = Object.getPrototypeOf(left)
  while (up) {
    if (up === right.prototype) {
      return true
    }
    up = Object.getPrototypeOf(up)
  }
  return false
}

function Person() { };
var p = new Person();
console.log(myInstanceof(p, Object), p instanceof Object);
console.log(myInstanceof(1, Number), 1 instanceof Number);
skyler-developer commented 3 months ago

function myInstanceof(left, right) {
    if (typeof left !== "object" || left === null) {
        return false;
    }
    let proto = Object.getPrototypeOf(left);
    while (proto) {
        if (proto === right.prototype) {
            return true;
        }
        proto = Object.getPrototypeOf(proto);
    }

    return false;
}
jianxingyao commented 2 months ago
function MyInstanceof(obj, constructorFunc){
    const _proto_ = Object.getPrototypeOf(obj);
    if(_proto_){
        if(_proto_ === constructorFunc.prototype){
            return true
        }
        return MyInstanceof(_proto_, constructorFunc)
    }
    else{
        return false
    }
}
Windseek commented 1 week ago

const myInstanceOf = (left, right) => { let proto = Object.getPrototypeOf(left); let prototype = right.prototype; return proto === prototype || myInstanceOf(proto, right); }

console.log(myInstanceOf([], Array));