深拷贝
递归
function deepClone(source
){
const targetObj
= source
.constructor
=== Array
? [] : {};
for(let keys
in source
){
if(source
.hasOwnProperty(keys
)){
if(source
[keys
] && typeof source
[keys
] === 'object'){
targetObj
[keys
] = source
[keys
].constructor
=== Array
? [] : {};
targetObj
[keys
] = deepClone(source
[keys
]);
}else{
targetObj
[keys
] = source
[keys
];
}
}
}
return targetObj
;
}
JSON.parse(JSON.stringify())
JSON.parse(JSON.stringify()) 方法局限
undefined、function、symbol 会在转换过程被忽略
转载请注明原文地址:https://tech.qufami.com/read-9776.html