一个简单的深拷贝函数
Opened this issue · 0 comments
chunhuigao commented
常用的方式,递归处理对象
function deepCopy(o) {
if (typeof o === 'object') {
const type = Object.prototype.toString.call(o);
switch (type) {
case '[object Object]':
const object = {};
Object.keys(o).forEach((key) => {
object[key] = deepCopy(o[key]);
});
return object;
case '[object Array]':
const list = [];
o.forEach((v, i) => {
list[i] = deepCopy(v);
});
return list;
case '[object Function]':
const fn = function () {};
fn.call(o);
return fn;
case '[object Null]':
return null;
case '[object Date]':
return new Date(o);
default:
break;
}
} else {
return o;
}
}