首页 > 试题广场 >

instanceof

[编程题]instanceof
  • 热度指数:10599 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
请补全JavaScript代码,要求以Boolean的形式返回第一个实例参数是否在第二个函数参数的原型链上。
const _instanceof = (target, Fn) => {
    // 补全代码
    return Fn.prototype.isPrototypeOf(target);
}

发表于 2021-12-17 12:10:32 回复(0)
在原型链上查找
使用接口是Object.getPrototypeOf()
先去除基本数据类型的干扰
在获取第一个类型的原型
用第一个类型的原型和第二个类型的原型进行比较
:如果相当 则返回true
:如果不相等 则第一个原型再往上找一层 再比较;
const _instanceof = (target, Fn) => {
    // 补全代码
        //基本数据类型的判断
    if(target === null || typeof target !== 'object'){
        return false
    }
        //获取第一个原型
    let leftProto = Object.getPrototypeOf(target)
    while(true){
        if (leftProto === null){
            return false
        }
                //和第二个原型进行比较
        if(leftProto == Fn.prototype){
            return true
        }

        leftProto = Object.getPrototypeOf(leftProto)
    }
}



发表于 2021-12-01 21:07:18 回复(0)
题目描述有问题,MDN中对于instanceof的定义是:
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
发表于 2022-06-04 00:01:51 回复(2)
const _instanceof = (target, Fn) => {
    // 补全代码
    return target instanceof Fn;
}

发表于 2022-01-10 19:30:22 回复(3)
 const _instanceof = (target, Fn) => {  
     while(target) {
         if(target.__proto__ === Fn.prototype) {
             return true;
         } 
         target = target.__proto__;
     }
     return false;

 }
如何判断对象是不是某一个构造函数的实例
target.__proto__ === Fn.prototype;  (一级原型链)
target.__proto__.__proto__  === Fn.prototype;
直到target.__proto__.__proto__.__proto__是 null, 说明没有在原型上找到。

其余获得实例对象的原型对象:
Object.prototypeOf(obj); 获取对象的原型对象 

发表于 2022-10-04 14:13:33 回复(0)
            const _instanceof = (target, Fn) => {
                // 补全代码
                return  Fn.prototype.isPrototypeOf(target)
                
            }

发表于 2022-08-08 14:51:37 回复(0)
/**
 * @param {Object} target 
 * @param {Function} Fn 
 * @returns {boolean}
 */
const _instanceof = (target, Fn) => {
  // 补全代码
  let proto = target.__proto__;
  while (proto) {
    if (proto === Fn.prototype) return true;
    proto = proto.__proto__;
  }
  return false;
};
实例原型链上查找,建议官方加入函数注释
发表于 2022-04-20 12:14:52 回复(0)
const _instanceof = (target, Fn) => {
    // 补全代码
    if(typeof target!== 'object' || target == null) return false;
    let proto = target.__proto__;
    while(true) {
        if(proto === null ) return false;
        if(proto === Fn.prototype) return true;
        proto = proto.__proto__
    }
}

发表于 2022-03-27 16:58:49 回复(0)
递归法
const _instanceof = (target, Fn) => {
            let ty = Object.getPrototypeOf(target)
            

            if(ty=== Fn.prototype) return true
            if(ty=== null) return false
            _instanceof(ty.__proto__, Fn)
   
}
循环法
const _instanceof = (target, Fn) => {
            let ty = Object.getPrototypeOf(target)
             
            while(ty){
                if(ty=== Fn.prototype) return true
                ty=ty.__proto__
            }
            return false
    
}


发表于 2023-08-10 10:28:15 回复(0)
不用instanceof的话。。
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            const _instanceof = (target, Fn) => {
                // 补全代码
                while(Fn.__proto__ !== Object.prototype){
                    if(target.__proto__ === Fn.prototype)
                        return true;
                    Fn = Fn.__proto__;
                }
                return false;
            }
        </script>
    </body>
</html>


发表于 2023-06-02 20:45:57 回复(0)
 const _instanceof = (target, Fn) => {
     //基本数据类型不能使用instanceof,检测的对象是非null的object类型
     if (target == "null" && target !== "object") return false
     //
     let proto = target.__proto__
     let prototype = Fn.prototype
     //如果proto和prototype是null
     while (prototype) {

         if (proto == prototype)
             return true
         prototype = prototype.prototype
     }
     return false
 }

发表于 2023-03-02 19:13:33 回复(0)
          //看到的一个老哥的代码:
                 var proto=target.__proto__;
                var prototype=Fn.prototype;
                while(true){
                    if(proto === prototype) return true;
                    if(proto === null) return false;
                    proto=proto.__proto__;
                }
发表于 2022-12-04 20:33:48 回复(0)
            const _instanceof = (target, Fn) => {
                let p1 = target.__proto__
                let p2 = Fn.prototype
                while(p1){
                    if(p1 === p2) return true
                    else p1 = p1.__proto__
                }
                return false
            }

发表于 2022-10-26 15:07:28 回复(0)
const _instanceof = (target, Fn) => {
                // 补全代码
                return Fn.prototype.isPrototypeOf(target);
            }

发表于 2022-07-21 21:20:15 回复(0)
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            const _instanceof = (target, Fn) => {
                // 补全代码
                // 1、获取到对象的原型
                let proto = target.__proto__, prototype = Fn.prototype
                // 2、循环判断
                while (proto) {
                    if (proto === prototype) return true
                    proto = proto.__proto__
                }
                return false
            }
        </script>
    </body>
</html>

发表于 2022-04-13 21:58:11 回复(0)
<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
        
        <script type="text/javascript">
            const _instanceof = (target, Fn) => {
                // 补全代码
                let prop = Fn.prototype
                if(target.__proto__ === prop){
                    return true
                }
                while(prop){
                    prop = prop.prototype
                    if(target.__proto__ === prop){
                        return true
                    }
                }
                return false
            }
        </script>
    </body>
</html>
发表于 2022-02-14 11:28:06 回复(0)
const _instanceof = (target, Fn) => {
    let implicit = target.__proto__;
    const display = Fn.prototype;
    while (implicit) {
        if (implicit === display) return true;
        implicit = implicit.__proto__;
    }
    return false;
};

发表于 2022-01-04 21:55:30 回复(0)