forked from myst729/JavaScript-Utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclone-object.js
More file actions
31 lines (25 loc) · 727 Bytes
/
clone-object.js
File metadata and controls
31 lines (25 loc) · 727 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
/* forked from zswang */
function cloneObject (obj) {
var i
var o
if (arguments.callee.caller === arguments.callee) { // run recursively
for (i = 0; i < arguments.callee.objList.length; i++) {
if (obj === arguments.callee.objList[i][0]) {
return arguments.callee.objList[i][1]
}
}
} else { // first run
arguments.callee.objList = []
}
o = obj instanceof Array ? [] : {}
arguments.callee.objList.push([obj, o])
for (i in obj) {
if (obj.hasOwnProperty(i)) {
o[i] = typeof obj[i] === 'object' ? arguments.callee(obj[i]) : obj[i]
}
}
if (arguments.callee.caller !== arguments.callee) { // end of first run
arguments.callee.objList = null
}
return o
}