1.判读是不是原始值
2.判读是数组还是对象
3.建立相应的数组/对象
递归
代码如下:
function deepClone(origin, target) {
var target = target || {},
toStr = Object.prototype.toString,
arrStr = '[Object Array]';
for (var prop in origin) {
if (origin.hasOwnProperty(prop)) {
if (typeof(origin[prop] == 'object') {
if (toStr.call(origin[prop] == arrStr)) {
target[prop] = []
} else {
target[prop] = {}
}
// 递归
deepClone(target[prop], origin[prop])
} else {
target[prop] = origin[prop]
}
}
}
}