{"id":239685,"date":"2022-04-18T09:54:24","date_gmt":"2022-04-18T01:54:24","guid":{"rendered":"https:\/\/gulass.cn\/?p=239685"},"modified":"2022-04-11T10:54:49","modified_gmt":"2022-04-11T02:54:49","slug":"implementing-deep-copy-requires-least-a-few-lines-code","status":"publish","type":"post","link":"https:\/\/gulass.cn\/implementing-deep-copy-requires-least-a-few-lines-code.html","title":{"rendered":"\u5b9e\u73b0\u6df1\u62f7\u8d1d\u6700\u5c11\u5f97\u7528\u51e0\u884c\u4ee3\u7801\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5bf9\u4e8e\u6df1\u62f7\u8d1d\u7684\u5b9e\u73b0\uff0c\u53ef\u80fd\u5b58\u5728\u5f88\u591a\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u5176\u539f\u7406\uff0c\u5e76\u80fd\u591f\u8bb0\u4f4f\u4e00\u79cd\u6700\u5bb9\u6613\u7406\u89e3\u548c\u5b9e\u73b0\u7684\u65b9\u5f0f\uff0c\u9762\u5bf9\u7c7b\u4f3c\u7684\u95ee\u9898\u624d\u80fd\u505a\u5230 \u4e34\u5371\u4e0d\u4e71\uff0c\u6cf0\u7136\u81ea\u82e5\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6df1\u5ea6\u514b\u9686(\u6df1\u62f7\u8d1d)\u4e00\u76f4\u90fd\u662f\u521d\u3001\u4e2d\u7ea7\u524d\u7aef\u9762\u8bd5\u4e2d\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u9898\u76ee\uff0c\u7f51\u4e0a\u4ecb\u7ecd\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e5f\u90fd\u5404\u6709\u5343\u79cb\uff0c\u5927\u4f53\u53ef\u4ee5\u6982\u62ec\u4e3a\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n

  • JSON.stringify+JSON.parse, \u8fd9\u4e2a\u5f88\u597d\u7406\u89e3\uff1b<\/li>\n
  • \u5168\u91cf\u5224\u65ad\u7c7b\u578b\uff0c\u6839\u636e\u7c7b\u578b\u505a\u4e0d\u540c\u7684\u5904\u7406\uff1b<\/li>\n
  • 2\u7684\u53d8\u578b\uff0c\u7b80\u5316\u7c7b\u578b\u5224\u65ad\u8fc7\u7a0b\u3002<\/li>\n

    \u524d\u4e24\u79cd\u6bd4\u8f83\u5e38\u89c1\u4e5f\u6bd4\u8f83\u57fa\u7840\uff0c\u6240\u4ee5\u6211\u4eec\u4eca\u5929\u4e3b\u8981\u8ba8\u8bba\u7684\u662f\u7b2c\u4e09\u79cd\u3002
    \n\"\"<\/p>\n

    \u95ee\u9898\u5206\u6790<\/strong><\/div>\n

    \u6df1\u62f7\u8d1d\u81ea\u7136\u662f\u76f8\u5bf9\u6d45\u62f7\u8d1d \u800c\u8a00\u7684\u3002\u6211\u4eec\u90fd\u77e5\u9053\u5f15\u7528\u6570\u636e\u7c7b\u578b \u53d8\u91cf\u5b58\u50a8\u7684\u662f\u6570\u636e\u7684\u5f15\u7528\uff0c\u5c31\u662f\u4e00\u4e2a\u6307\u5411\u5185\u5b58\u7a7a\u95f4\u7684\u6307\u9488\uff0c \u6240\u4ee5\u5982\u679c\u6211\u4eec\u50cf\u8d4b\u503c\u7b80\u5355\u6570\u636e\u7c7b\u578b\u90a3\u6837\u7684\u65b9\u5f0f\u8d4b\u503c\u7684\u8bdd\uff0c\u5176\u5b9e\u53ea\u80fd\u590d\u5236\u4e00\u4e2a\u6307\u9488\u5f15\u7528\uff0c\u5e76\u6ca1\u6709\u5b9e\u73b0\u771f\u6b63\u7684\u6570\u636e\u514b\u9686\u3002<\/p>\n

    \u901a\u8fc7\u8fd9\u4e2a\u4f8b\u5b50\u5f88\u5bb9\u6613\u5c31\u80fd\u7406\u89e3\uff1a<\/p>\n

    \r\nconst obj1 = {\r\n    name: 'superman'\r\n}\r\nconst obj2 = obj1;\r\nobj1.name = '\u524d\u7aef\u5207\u56fe\u4ed4';\r\nconsole.log(obj2.name); \/\/ \u524d\u7aef\u5207\u56fe\u4ed4\r\n<\/pre>\n

    \u6240\u4ee5\u6df1\u5ea6\u514b\u9686\u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u5f15\u7528\u6570\u636e\u7c7b\u578b\u4e0d\u80fd\u88ab\u901a\u8fc7\u8d4b\u503c\u7684\u65b9\u5f0f \u590d\u5236 \u7684\u95ee\u9898\u3002<\/p>\n

    \u5f15\u7528\u6570\u636e\u7c7b\u578b<\/strong><\/div>\n

    \u6211\u4eec\u4e0d\u59a8\u6765\u7f57\u5217\u4e00\u4e0b\u5f15\u7528\u6570\u636e\u7c7b\u578b\u90fd\u6709\u54ea\u4e9b\uff1a<\/p>\n

    ES6\u4e4b\u524d\uff1a\u5bf9\u8c61\uff0c \u6570\u7ec4\uff0c \u65e5\u671f\uff0c \u6b63\u5219\u8868\u8fbe\u5f0f\uff0c \u9519\u8bef\uff0c
    \nES6\u4e4b\u540e\uff1aMap\uff0c Set\uff0c WeakMap\uff0c WeakSet\uff0c
    \n\u6240\u4ee5\uff0c\u6211\u4eec\u8981\u6df1\u5ea6\u514b\u9686\uff0c\u5c31\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u904d\u5386\u5e76\u6839\u636e\u7c7b\u578b\u91c7\u53d6\u76f8\u5e94\u7684\u514b\u9686\u65b9\u5f0f\u3002\u5f53\u7136\u56e0\u4e3a\u6570\u636e\u4f1a\u5b58\u5728\u591a\u5c42\u5d4c\u5957\u7684\u60c5\u51b5\uff0c\u91c7\u7528\u9012\u5f52\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n

    \u7b80\u5355\u7c97\u66b4\u7248\u672c<\/strong><\/div>\n
    \r\nfunction deepClone(obj) {\r\n    let res = {};\r\n    \/\/ \u7c7b\u578b\u5224\u65ad\u7684\u901a\u7528\u65b9\u6cd5\r\n    function getType(obj) {\r\n        return Object.prototype.toString.call(obj).replaceAll(new RegExp(\/\\[|\\]|object \/g), \"\");\r\n    }\r\n    const type = getType(obj);\r\n    const reference = [\"Set\", \"WeakSet\", \"Map\", \"WeakMap\", \"RegExp\", \"Date\", \"Error\"];\r\n    if (type === \"Object\") {\r\n        for (const key in obj) {\r\n            if (Object.hasOwnProperty.call(obj, key)) {\r\n                res[key] = deepClone(obj[key]);\r\n            }\r\n        }\r\n    } else if (type === \"Array\") {\r\n        console.log('array obj', obj);\r\n        obj.forEach((e, i) => {\r\n            res[i] = deepClone(e);\r\n        });\r\n    }\r\n    else if (type === \"Date\") {\r\n        res = new Date(obj);\r\n    } else if (type === \"RegExp\") {\r\n        res = new RegExp(obj);\r\n    } else if (type === \"Map\") {\r\n        res = new Map(obj);\r\n    } else if (type === \"Set\") {\r\n        res = new Set(obj);\r\n    } else if (type === \"WeakMap\") {\r\n        res = new WeakMap(obj);\r\n    } else if (type === \"WeakSet\") {\r\n        res = new WeakSet(obj);\r\n    }else if (type === \"Error\") {\r\n        res = new Error(obj);\r\n    }\r\n     else {\r\n        res = obj;\r\n    }\r\n    return res;\r\n}\r\n<\/pre>\n

    \u5176\u5b9e\u8fd9\u5c31\u662f\u6211\u4eec\u6700\u524d\u9762\u63d0\u5230\u7684\u7b2c\u4e8c\u79cd\u65b9\u5f0f\uff0c\u5f88\u50bb\u5bf9\u4e0d\u5bf9\uff0c\u660e\u773c\u4eba\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u6765\u6709\u5f88\u591a\u5197\u4f59\u4ee3\u7801\u53ef\u4ee5\u5408\u5e76\u3002<\/p>\n

    \u6211\u4eec\u5148\u8fdb\u884c\u6700\u57fa\u672c\u7684\u4f18\u5316\uff1a<\/p>\n

    \u5408\u5e76\u5197\u4f59\u4ee3\u7801<\/strong><\/div>\n

    \u5c06\u4e00\u773c\u5c31\u80fd\u770b\u51fa\u6765\u5197\u4f59\u7684\u4ee3\u7801\u5408\u5e76\u4e0b\u3002<\/p>\n

    \r\nfunction deepClone(obj) {\r\n    let res = null;\r\n    \/\/ \u7c7b\u578b\u5224\u65ad\u7684\u901a\u7528\u65b9\u6cd5\r\n    function getType(obj) {\r\n        return Object.prototype.toString.call(obj).replaceAll(new RegExp(\/\\[|\\]|object \/g), \"\");\r\n    }\r\n    const type = getType(obj);\r\n    const reference = [\"Set\", \"WeakSet\", \"Map\", \"WeakMap\", \"RegExp\", \"Date\", \"Error\"];\r\n    if (type === \"Object\") {\r\n        res = {};\r\n        for (const key in obj) {\r\n            if (Object.hasOwnProperty.call(obj, key)) {\r\n                res[key] = deepClone(obj[key]);\r\n            }\r\n        }\r\n    } else if (type === \"Array\") {\r\n        console.log('array obj', obj);\r\n        res = [];\r\n        obj.forEach((e, i) => {\r\n            res[i] = deepClone(e);\r\n        });\r\n    }\r\n    \/\/ \u4f18\u5316\u6b64\u90e8\u5206\u5197\u4f59\u5224\u65ad\r\n    \/\/ else if (type === \"Date\") {\r\n    \/\/     res = new Date(obj);\r\n    \/\/ } else if (type === \"RegExp\") {\r\n    \/\/     res = new RegExp(obj);\r\n    \/\/ } else if (type === \"Map\") {\r\n    \/\/     res = new Map(obj);\r\n    \/\/ } else if (type === \"Set\") {\r\n    \/\/     res = new Set(obj);\r\n    \/\/ } else if (type === \"WeakMap\") {\r\n    \/\/     res = new WeakMap(obj);\r\n    \/\/ } else if (type === \"WeakSet\") {\r\n    \/\/     res = new WeakSet(obj);\r\n    \/\/ }else if (type === \"Error\") {\r\n    \/\/   res = new Error(obj);\r\n    \/\/}\r\n    else if (reference.includes(type)) {\r\n        res = new obj.constructor(obj);\r\n    } else {\r\n        res = obj;\r\n    }\r\n    return res;\r\n}\r\n<\/pre>\n

    \u4e3a\u4e86\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff0c\u6211\u4eec\u7528\u4e0b\u9762\u8fd9\u4e2a\u6570\u636e\u9a8c\u8bc1\u4e0b\uff1a<\/p>\n

    \r\nconst map = new Map();\r\nmap.set(\"key\", \"value\");\r\nmap.set(\"ConardLi\", \"coder\");\r\n\r\nconst set = new Set();\r\nset.add(\"ConardLi\");\r\nset.add(\"coder\");\r\n\r\nconst target = {\r\n    field1: 1,\r\n    field2: undefined,\r\n    field3: {\r\n        child: \"child\",\r\n    },\r\n    field4: [2, 4, 8],\r\n    empty: null,\r\n    map,\r\n    set,\r\n    bool: new Boolean(true),\r\n    num: new Number(2),\r\n    str: new String(2),\r\n    symbol: Object(Symbol(1)),\r\n    date: new Date(),\r\n    reg: \/\\d+\/,\r\n    error: new Error(),\r\n    func1: () => {\r\n        let t = 0;\r\n        console.log(\"coder\", t++);\r\n    },\r\n    func2: function (a, b) {\r\n        return a + b;\r\n    },\r\n};\r\n\/\/\u6d4b\u8bd5\u4ee3\u7801\r\nconst test1 = deepClone(target);\r\ntarget.field4.push(9);\r\nconsole.log('test1: ', test1);\r\n<\/pre>\n

    \u6267\u884c\u7ed3\u679c\uff1a
    \n\"\"<\/p>\n

    \u8fd8\u6709\u8fdb\u4e00\u6b65\u4f18\u5316\u7684\u7a7a\u95f4\u5417\uff1f<\/strong><\/span><\/div>\n

    \u7b54\u6848\u5f53\u7136\u662f\u80af\u5b9a\u7684\u3002<\/p>\n

    \r\n\/\/ \u5224\u65ad\u7c7b\u578b\u7684\u65b9\u6cd5\u79fb\u5230\u5916\u90e8\uff0c\u907f\u514d\u9012\u5f52\u8fc7\u7a0b\u4e2d\u591a\u6b21\u6267\u884c\r\nconst judgeType = origin => {\r\n    return Object.prototype.toString.call(origin).replaceAll(new RegExp(\/\\[|\\]|object \/g), \"\");\r\n};\r\nconst reference = [\"Set\", \"WeakSet\", \"Map\", \"WeakMap\", \"RegExp\", \"Date\", \"Error\"];\r\nfunction deepClone(obj) {\r\n    \/\/ \u5b9a\u4e49\u65b0\u7684\u5bf9\u8c61\uff0c\u6700\u540e\u8fd4\u56de\r\n     \/\/\u901a\u8fc7 obj \u7684\u539f\u578b\u521b\u5efa\u5bf9\u8c61\r\n    const cloneObj = Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));\r\n\r\n    \/\/ \u904d\u5386\u5bf9\u8c61\uff0c\u514b\u9686\u5c5e\u6027\r\n    for (let key of Reflect.ownKeys(obj)) {\r\n        const val = obj[key];\r\n        const type = judgeType(val);\r\n        if (reference.includes(type)) {\r\n            newObj[key] = new val.constructor(val);\r\n        } else if (typeof val === \"object\" && val !== null) {\r\n            \/\/ \u9012\u5f52\u514b\u9686\r\n            newObj[key] = deepClone(val);\r\n        } else {\r\n            \/\/ \u57fa\u672c\u6570\u636e\u7c7b\u578b\u548cfunction\r\n            newObj[key] = val;\r\n        }\r\n    }\r\n    return newObj;\r\n}\r\n<\/pre>\n

    \u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a
    \n\"\"<\/p>\n

  • Object.getOwnPropertyDescriptors() \u65b9\u6cd5\u7528\u6765\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u81ea\u8eab\u5c5e\u6027\u7684\u63cf\u8ff0\u7b26\u3002<\/li>\n
  • \u8fd4\u56de\u6240\u6307\u5b9a\u5bf9\u8c61\u7684\u6240\u6709\u81ea\u8eab\u5c5e\u6027\u7684\u63cf\u8ff0\u7b26\uff0c\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u81ea\u8eab\u5c5e\u6027\uff0c\u5219\u8fd4\u56de\u7a7a\u5bf9\u8c61\u3002<\/li>\n

    \u8fd9\u6837\u505a\u7684\u597d\u5904\u5c31\u662f\u80fd\u591f\u63d0\u524d\u5b9a\u4e49\u597d\u6700\u540e\u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n

    \u8fd9\u4e2a\u5b9e\u73b0\u53c2\u8003\u4e86\u7f51\u4e0a\u4e00\u4f4d\u5927\u4f6c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4e2a\u4eba\u89c9\u5f97\u7406\u89e3\u6210\u672c\u6709\u70b9\u9ad8\uff0c\u800c\u4e14\u5bf9\u6570\u7ec4\u7c7b\u578b\u7684\u5904\u7406\u4e5f\u4e0d\u662f\u7279\u522b\u4f18\u96c5, \u8fd4\u56de\u7c7b\u6570\u7ec4\u3002<\/p>\n

    \u6211\u5728\u6211\u4e0a\u9762\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u6539\u9020\uff0c\u6539\u9020\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    \r\nfunction deepClone(obj) {\r\n    let res = null;\r\n    const reference = [Date, RegExp, Set, WeakSet, Map, WeakMap, Error];\r\n    if (reference.includes(obj?.constructor)) {\r\n        res = new obj.constructor(obj);\r\n    } else if (Array.isArray(obj)) {\r\n        res = [];\r\n        obj.forEach((e, i) => {\r\n            res[i] = deepClone(e);\r\n        });\r\n    } else if (typeof obj === \"Object\" && obj !== null) {\r\n        res = {};\r\n        for (const key in obj) {\r\n            if (Object.hasOwnProperty.call(obj, key)) {\r\n                res[key] = deepClone(obj[key]);\r\n            }\r\n        }\r\n    } else {\r\n        res = obj;\r\n    }\r\n    return res;\r\n}\r\n<\/pre>\n

    \u867d\u7136\u4ee3\u7801\u91cf\u4e0a\u6ca1\u6709\u4ec0\u4e48\u4f18\u52bf\uff0c\u4f46\u662f\u6574\u4f53\u7684\u7406\u89e3\u6210\u672c\u548c\u4f60\u6e05\u6670\u5ea6\u4e0a\u6211\u89c9\u5f97\u4f1a\u66f4\u597d\u4e00\u70b9\u3002\u90a3\u4e48\u4f60\u89c9\u5f97\u5462\uff1f<\/p>\n

    \u6700\u540e\uff0c\u8fd8\u6709\u5faa\u73af\u5f15\u7528\u95ee\u9898\uff0c\u907f\u514d\u51fa\u73b0\u65e0\u7ebf\u5faa\u73af\u7684\u95ee\u9898\u3002<\/p>\n

    \u6211\u4eec\u7528hash\u6765\u5b58\u50a8\u5df2\u7ecf\u52a0\u8f7d\u8fc7\u7684\u5bf9\u8c61\uff0c\u5982\u679c\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u5c31\u76f4\u63a5\u8fd4\u56de\u3002<\/p>\n

    \r\nfunction deepClone(obj, hash = new WeakMap()) {\r\n    if (hash.has(obj)) {\r\n        return obj;\r\n    }\r\n    let res = null;\r\n    const reference = [Date, RegExp, Set, WeakSet, Map, WeakMap, Error];\r\n\r\n    if (reference.includes(obj?.constructor)) {\r\n        res = new obj.constructor(obj);\r\n    } else if (Array.isArray(obj)) {\r\n        res = [];\r\n        obj.forEach((e, i) => {\r\n            res[i] = deepClone(e);\r\n        });\r\n    } else if (typeof obj === \"Object\" && obj !== null) {\r\n        res = {};\r\n        for (const key in obj) {\r\n            if (Object.hasOwnProperty.call(obj, key)) {\r\n                res[key] = deepClone(obj[key]);\r\n            }\r\n        }\r\n    } else {\r\n        res = obj;\r\n    }\r\n    hash.set(obj, res);\r\n    return res;\r\n}\r\n<\/pre>\n
    \u603b\u7ed3<\/strong><\/div>\n

    \u5bf9\u4e8e\u6df1\u62f7\u8d1d\u7684\u5b9e\u73b0\uff0c\u53ef\u80fd\u5b58\u5728\u5f88\u591a\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5173\u952e\u5728\u4e8e\u7406\u89e3\u5176\u539f\u7406\uff0c\u5e76\u80fd\u591f\u8bb0\u4f4f\u4e00\u79cd\u6700\u5bb9\u6613\u7406\u89e3\u548c\u5b9e\u73b0\u7684\u65b9\u5f0f\uff0c\u9762\u5bf9\u7c7b\u4f3c\u7684\u95ee\u9898\u624d\u80fd\u505a\u5230 \u4e34\u5371\u4e0d\u4e71\uff0c\u6cf0\u7136\u81ea\u82e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6df1\u5ea6\u514b\u9686(\u6df1\u62f7\u8d1d)\u4e00\u76f4\u90fd\u662f\u521d\u3001\u4e2d\u7ea7\u524d\u7aef\u9762\u8bd5\u4e2d\u7ecf\u5e38\u88ab\u95ee\u5230\u7684\u9898\u76ee\uff0c\u7f51\u4e0a\u4ecb\u7ecd\u7684\u5b9e\u73b0\u65b9\u5f0f\u4e5f\u90fd\u5404\u6709\u5343\u79cb\uff0c\u5927\u4f53\u53ef\u4ee5\u6982\u62ec\u4e3a\u4e09 […]<\/p>\n","protected":false},"author":1898,"featured_media":239689,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[836],"class_list":["post-239685","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-836"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/239685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=239685"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/239685\/revisions"}],"predecessor-version":[{"id":239691,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/239685\/revisions\/239691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/239689"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=239685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=239685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=239685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}