功能如下:
- WeakMap 解决循环引用问题
- 支持拷贝的数据类型:
-
- 基本数据类型: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);
标签:instanceof,map,set,obj,js,result,new,拷贝 From: https://www.cnblogs.com/dadouF4/p/18365180