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) } }
const _instanceof = (target, Fn) => { while(target) { if(target.__proto__ === Fn.prototype) { return true; } target = target.__proto__; } return false; }
const _instanceof = (target, Fn) => { // 补全代码 return Fn.prototype.isPrototypeOf(target) }
/** * @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; };实例原型链上查找,建议官方加入函数注释
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 }
<!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>
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 }
<!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>