1.js的instanceOf的实现

43 阅读1分钟
function myInstanceof(obj,constructor) {
    let prototype = constructor.prototype;
    if (obj === null || obj === undefined || !obj || (typeof obj !== 'object' && typeof obj !== 'function')) {
        return false;
    }
    let proto = obj.__proto__;
    // let proto = Object.getPrototypeOf(left)  //写法2 获取左边对象的原型
    while (true) {
        if (proto == null) return false;
        if (proto === prototype) return true;
        proto = proto.__proto__;
    }
}
var a={"qqq":"111","22":"ee"};
console.log(myInstanceof(a,Object))