-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepCopyObject.js
More file actions
39 lines (38 loc) · 897 Bytes
/
deepCopyObject.js
File metadata and controls
39 lines (38 loc) · 897 Bytes
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
31
32
33
34
35
36
37
38
39
/*
* @author joke
* @time 2020-05-26 09:47:44 周二
* @desc 对象深拷贝
*/
function _deepCopy(obj) {
let result = {};
let instace = _isClass(obj);
if (instace === "RegExp") {
// regexp
result = new RegExp(obj.toString().slice(1, -1));
} else if (instace === "Date") {
// date
result = new Date(obj);
} else {
// object array
for (let key in obj) {
let temp = obj[key];
let type = _isClass(temp);
if (type === "Object") {
result[key] = _deepCopy(temp);
} else if (type === "Array") {
result[key] = _copyArray(temp);
} else {
result[key] = temp;
}
}
}
return result;
}
function _copyArray(arr) {
return arr.concat();
}
function _isClass(obj) {
if (obj === null) return "Null";
if (obj === undefined) return "Undefined";
return Object.prototype.toString.call(obj).slice(8, -1);
}