列表

详情


FED13. new

描述

请补全JavaScript代码,要求实现new操作符的功能。

示例1

输入:

const object = _new(Fn)

输出:

原站题解

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

{"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 _new = function() {\n                // 补全代码\n                 const object1 = {}\n    const fn = arguments[0]\n    object1.__proto__ = fn.prototype\n    const object2 = fn.apply(object1, arguments)\n    return object2 instanceof Object ? object2 : object1\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1687ms, 内存消耗: 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 _new = function(constructor,...args) {\n                //创建要给新对象\n                const obj = {}\n                //为新对象添加属性__proto__\n                obj.__proto__ = constructor.prototype \n                //执行构造函数 将this指向新对象\n                const res = constructor.apply(obj,args)\n                //确保返回一个对象\n                return res instanceof Object ? res : obj\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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 _new = function (constructor, ...args) {\n                // 补全代码\n                let obj = Object.create(constructor.prototype);\n                let res = constructor.apply(obj, args);\n                return res instanceof Object ? res : obj;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77776KB, 提交时间: 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 _new = function() {\n                // 补全代码\n                let newObj1 = {}\n                let Fn = [...arguments].shift()\n                newObj1.__proto__ = Fn.prototype\n                let newObj2 = Fn.call(newObj1,...arguments)\n                return newObj2 instanceof Object ? newObj2 : newObj1\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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 _new = function() {\n                // 补全代码\n                let constructor = [].shift.call(arguments);\n                var obj = Object.create(constructor.prototype);\n                var res = constructor.apply(obj,arguments);\n                return res instanceof Object? res : obj;\n                \n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题