共夸不受污泥染,没有污泥哪有莲。——《赏荷》

对象深拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
export function deepClone (data) {
var type = getObjType(data)
var obj
if (type === 'array') {
obj = []
} else if (type === 'object') {
obj = {}
} else {
// 不再具有下一层次
return data
}
if (type === 'array') {
for (var i = 0, len = data.length; i < len; i++) {
data[i] = (function () {
if (data[i] === 0) {
return data[i]
}
return data[i]
}())
delete data[i].$parent
obj.push(deepClone(data[i]))
}
} else if (type === 'object') {
for (var key in data) {
delete data.$parent
obj[key] = deepClone(data[key])
}
}
return obj
};