d-utils
Version:
d-utils
25 lines (24 loc) • 997 B
JavaScript
/**
* d-utils version: 4.0.3
* by ifmiss
*/
import e from"./checkType.js";import{b as r}from"./_tslib-05983193.js";
/**
* @description 深拷贝
* @param { Object } obj 被拷贝的对象
* @return { Object } 返回新的对象
* @example
* let a = {
* a: 1,
* b: 2,
* c: 3,
* d: [1, 2]
* }
* let b = deepClone(a)
* a.d[0] = 3
* console.log(a)
* // a: {a: 1, b: 2, c: 3, d: [3, 2]}
* console.log(b)
* // b: {a: 1, b: 2, c: 3, d: [1, 2]}
* // 此时修改a.d[0]的值, a对象变化了,b对象没有随之改变
*/export default function a(o){var t,c;console.warn("deepClone 方法暂时没有做对象引用的优化,可食用 lodash 的 cloneDeep 方法");var n={},l=Object.keys(o);try{for(var s=r(l),f=s.next();!f.done;f=s.next()){var i=f.value;switch(e(o[i])){case"object":n[i]=a(o[i]);break;case"array":n[i]=[].concat(o[i]);break;default:n[i]=o[i]}}}catch(e){t={error:e}}finally{try{f&&!f.done&&(c=s.return)&&c.call(s)}finally{if(t)throw t.error}}return n}