function deepClone(origin, target) {
// 遍历对象: for(var i in obj)
// 1.判断是不是原始值 typeof() Object
// 2.判断是数组还是对象 instanceof tostring constructor
// 3.建立相应的数组或对象
var target = target || {},
tostr = Object.prototype.toString
arrstr = '[object,Array]';
for (var prop in origin) {
if (origin.hasOwnProperty(prop)) {
if (typeof (prop) != 'Object' && origin[prop] !== 'null') {
target[prop] = origin[prop];
}
else {
if (tostr.call(prop) == arrstr) {
target[prop] = [];
}
else {
target[prop] = {};
}
deepClone(origin[prop], target[prop]);
}
}
}
return target;
}
深拷贝 js
最新推荐文章于 2019-09-19 11:49:42 发布