列表

详情


FED6. instanceof

描述

请补全JavaScript代码,要求以Boolean的形式返回第一个实例参数是否在第二个函数参数的原型链上。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1685ms, 内存消耗: 77808KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _instanceof = (target, Fn) => {\n                // 补全代码\n                while(target){\n                    if(target.__proto__ === Fn.prototype) return true;\n                    target = target.__proto__;\n                }\n                return false;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1686ms, 内存消耗: 77812KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _instanceof = (target, Fn) => {\n                // 补全代码\n                if(target === null || typeof target !== 'object') {\n                    return false\n                }\n                let proto = Object.getPrototypeOf(target);\n                while(true) {\n                    if(proto === null) {\n                        return false\n                    }\n                    if (proto == Fn.prototype){\n                        return true\n                    }\n                    proto = Object.getPrototypeOf(proto);\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1736ms, 内存消耗: 77820KB, 提交时间: 2021-12-07

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _instanceof = (target, Fn) => {\n                // 补全代码\n                //基本数据类型的判断\n                if(target === null || typeof target !== 'object'){\n                    return false\n                }\n                //获取第一个原型\n                let leftProto = Object.getPrototypeOf(target)\n                while(true){\n                    if (leftProto === null){\n                        return false\n                    }\n                            //和第二个原型进行比较\n                    if(leftProto == Fn.prototype){\n                        return true\n                    }\n\n                    leftProto = Object.getPrototypeOf(leftProto)\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77772KB, 提交时间: 2022-01-03

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _instanceof = (target, Fn) => {\n                // 补全代码\n                let leftProto = target.__proto__\n                let rightProto = Fn.prototype\n                while(true){\n                    if(!rightProto){\n                        return false\n                    }\n                    if(leftProto==rightProto){\n                        return true\n                    }else{\n                        rightProto = rightProto.__proto__\n                    }\n                }\n                \n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77772KB, 提交时间: 2021-12-31

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _instanceof = (target, Fn) => {\n                // 补全代码\n                if (target === null || typeof target !== 'object') {\n                      return false;\n                }\n                let proto = Object.getPrototypeOf(target);\n                let protoType = Fn.prototype;\n                while(true) {\n                    if (proto === protoType) return true;\n                    if (proto === null) return false;\n                    proto = Object.getPrototypeOf(proto);\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题