https://github.com/aweiu/clone
js对象深拷贝demo
https://github.com/aweiu/clone
Last synced: 3 months ago
JSON representation
js对象深拷贝demo
- Host: GitHub
- URL: https://github.com/aweiu/clone
- Owner: aweiu
- Created: 2018-10-14T11:36:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-14T12:22:53.000Z (over 6 years ago)
- Last Synced: 2025-01-06T10:13:29.302Z (5 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# clone
js对象深拷贝demo,测试用```
function isObject (data) {
return Object.prototype.toString.call(data) === '[object Object]'
}function clone (obj) {
const root = {}
const tmp = [
{
parent: root,
data: obj
}
]
let i = 0
while (i < tmp.length) {
const source = tmp[i++]
const { data, parent } = source
for (let o in data) {
if (data.hasOwnProperty(o)) {
if (isObject(data[o])) {
const _cloned = tmp.find(source => source.data === data[o])
if (_cloned) parent[o] = _cloned.parent
else tmp.push({ parent: parent[o] = {}, data: data[o] })
} else parent[o] = data[o]
}
}
}
return root
}const a = { b: { a: { c: 1 } }, d: 8 }
a.a = a
a.b.b = a.b
const b = clone(a)
b.b.b.a = 1
b.a.d = 9
console.log(b)
console.log(a)
```