列表

详情


FED17. 深拷贝

描述

请补全JavaScript代码,要求实现对象参数的深拷贝并返回拷贝之后的新对象。
注意:
1. 需要考虑函数、正则、日期、ES6新对象
2. 需要考虑循环引用问题

原站题解

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

{"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 _completeDeepClone = (target, map = new Map()) => {\n                // 补全代码\n                if(target === null || typeof target !== 'object') return target\n                const constructor = target.constructor\n                if(/^(Function|Date|RegExp|Map|Set)$/i.test(constructor.name)) return new constructor(target)\n                if(map.get(target)) return map.get(target)\n                map.set(target, true)\n                const clone = Array.isArray(target) ? [] : {}\n                for(let key in target) {\n                    if(Object.hasOwnProperty(key)) {\n                        clone[key] = _completeDeepClone(target[key])\n                    }\n                }\n                return clone\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"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 _completeDeepClone = (target, map = new Map()) => {\n                // 补全代码\n                  if (target === null || target === void 0) {\n                    return target;\n                  }\n                  const objConstructor = target.constructor;\n                  if (typeof target !== \"object\") {\n                    return target;\n                  } else if (objConstructor === Date) {\n                    return new Date(target.getTime());\n                  } else if (objConstructor === RegExp) {\n                    return new objConstructor(target);\n                  }\n\n                  const hit = map.get(target);\n                  if (hit) {\n                    return hit;\n                  }\n\n                  const copy = Array.isArray(target) ? [] : {};\n\n                  map.set(target, copy);\n\n                  Object.keys(target).forEach((key) => {\n                    copy[key] = _completeDeepClone(target[key], map);\n                  });\n\n                  return copy;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 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 _completeDeepClone = (target, map = new Map()) => {\n                // 补全代码\n                if(typeof target!=='object'||target===null){\n                    return target\n                }\n                const constructor=target.constructor\n                if(/^(Function|RegExp|Date|Set|Map)$/i.test(constructor.name)){\n                    return new constructor(target)\n                }\n                if(map.get(target)){\n                    return map.get(target)\n                }\n                map.set(target,true)\n                let clone=Array.isArray(target)?[]:{}\n                for(prop in target){\n                    if(target.hasOwnProperty(prop)){\n                        clone[prop]=_completeDeepClone(target[prop],map)\n                    }\n                }\n                return clone\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

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

{"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 _completeDeepClone = (target, map = new Map()) => {\n  if (typeof target !== \"object\" || target === null) return target;\n\n  const constructor = target.constructor;\n  if (/^(Function|RegExp|Date|Map|Set)$/i.test(constructor.name)) {\n    return new constructor(target);\n  }\n\n  if (map.get(target)) {\n    return map.get(target);\n  }\n\n  map.set(target, true);\n  let resData = Array.isArray(target) ? [] : {};\n\n  for (const key in target) {\n    if (target.hasOwnProperty(key)) {\n      resData[key] = _completeDeepClone(target[key], map);\n    }\n  }\n  return resData;\n};\n\n        </script>\n    </body>\n</html>","libs":[]}

上一题