JS深拷贝
作者:
whyexist | 来源:发表于
2018-03-20 10:18 被阅读0次// 递归实现一个深拷贝
function deepClone(source) {
if (!source || typeof source !== 'object') {
throw new Error('error arguments', 'shallowClone');
}
var targetObj = source.constructor === Array ? [] : {};
for (var 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;
}
本文标题:JS深拷贝
本文链接:https://www.haomeiwen.com/subject/rbsvqftx.html
网友评论