/**
* 深拷贝
*
* @param source 要复制的内容
*/
export function deepCopy(source) {
if (!isObject(source)) return source; //如果不是对象的话直接返回
let target = Array.isArray(source) ? [] : {} //数组兼容
for (var k in source) {
if (source.hasOwnProperty(k)) {
if (typeof source[k] === 'object') {
target[k] = deepCopy(source[k])
} else {
target[k] = source[k]
}
}
}
return target
}
function isObject(obj) {
return typeof obj === 'object' && obj !== null
}
有的场景直接赋值后,修改一个变量另一个也改了,这不符合需求,可以考虑使用深拷贝