js深拷贝

功能如下:

  1. WeakMap 解决循环引用问题
  2. 支持拷贝的数据类型:
    • 基本数据类型:number、string、boolean、undefined、null、symbol
    • 引用数据类型:Date、RegExp、Function、Object、Map、Set
代码实现:
  
复制代码
 1 function clone(obj, map = new WeakMap) {
 2     if (map.has(obj)) return map.get(obj);
 3     if (obj === null) {
 4         return null;
 5     }
 6     let result = {};
 7     if (Array.isArray(obj)) {
 8         result = [];
 9     }
10     if (obj instanceof RegExp || obj instanceof Date) {
11         result = new obj.constructor(obj);
12         map.set(obj, result);
13     }
14     if (obj instanceof Map || obj instanceof Set) {
15         result = new obj.constructor();
16         map.set(obj, result)
17         if (obj instanceof Map) {
18             obj.forEach((val, key) => {
19                 result.set(key, clone(val, map));
20             });
21         } else {
22             obj.forEach((val, key) => {
23                 result.add(clone(val, map));
24             });
25         }
26         // 由于Map、Set的typeof 为object,这里需要return;
27         return result;
28     }
29     if (obj instanceof Function) {
30         result = () => { obj() };
31         map.set(obj, result)
32     }
33     if (['number', 'string', 'boolean'].includes(typeof obj)) {
34         result = obj;
35     }
36     if (typeof obj === 'object') {
37         result = Object.create(Reflect.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj), Object.getOwnPropertySymbols(obj));
38         map.set(obj, result)
39         Reflect.ownKeys(obj).forEach(key => {
40             result[key] = clone(obj[key], map);
41         });
42     }
43     return result;
44 }
复制代码

测试代码:

复制代码
 1 const mapA = new Map();
 2 mapA.set('a', 'aaa');
 3 const setA = new Set();
 4 setA.add(1,2,3);
 5 const sym = Symbol('m');
 6 const obj = {
 7     a: 1,
 8     b: '1',
 9     c: true,
10     d: undefined,
11     e: null,
12     f: new Date(),
13     g: new RegExp(),
14     h: function(){
15         console.log('111')
16     },
17     i: {i: '1', ii: {ii: '11'}},
18     j: [1,2,3, {j: 'j', jj: 'jj'}],
19     k: mapA,
20     l: setA,
21     [sym]: 'm'
22 }
23 const newObj = clone(obj);
复制代码

 

posted @   大豆F4  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示