列表

详情


FED15. 浅拷贝

描述

请补全JavaScript代码,要求实现一个对象参数的浅拷贝并返回拷贝之后的新对象。
注意:
1. 参数可能包含函数、正则、日期、ES6新对象

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 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 _shallowClone = target => {\n    // 补全代码\n    let copyOne=Array.isArray(target)?[]:{}\n    for(let key in target){\n        let value=target[key]\n        copyOne[key]=value\n    }\n    return copyOne\n}\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77820KB, 提交时间: 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 _shallowClone = target => {\n                // 补全代码\n                if(typeof target === 'object' && target !== null) {\n        const constructor = target.constructor\n        if(/^(Function|RegExp|Date|Map|Set)$/i.test(constructor.name)) return target\n        const cloneTarget = Array.isArray(target) ? [] : {}\n        for(prop in target) {\n            if(target.hasOwnProperty(prop)) {\n                cloneTarget[prop] = target[prop]\n            }\n        }\n        return cloneTarget\n    } else {\n        return target\n    }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 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 _shallowClone = target => {\n                // 补全代码\n                const obj = {};\n                for(const key in target){\n                    if(target.hasOwnProperty(key)){\n                        obj[key] = target[key]\n                    }\n                }\n                \n                return obj\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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 _shallowClone = target => {\n                // 补全代码\n                let copy = Array.isArray(target)?[]:{}\n                for(let key in target){\n                    let value = target[key]\n                    copy[key] = value\n                }\n                return copy\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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 _shallowClone = target => {\n                // 补全代码\n                return Object.assign({}, target)\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题