Skip to content
This repository was archived by the owner on Feb 13, 2022. It is now read-only.

Commit 9d21703

Browse files
committed
solution wesbos#14
1 parent 8040cad commit 9d21703

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

14 - JavaScript References VS Copying/index-START.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@
4545
// We will hopefully soon see the object ...spread
4646

4747
// Things to note - this is only 1 level deep - both for Arrays and Objects. lodash has a cloneDeep method, but you should think twice before using it.
48+
const person = {
49+
name: 'Rustam',
50+
age: 36,
51+
social: {
52+
www: 'rustamyusupov.name',
53+
github: 'github.com/rustamyusupov'
54+
}
55+
}
56+
57+
const person2 = JSON.parse(JSON.stringify(person));
58+
person2.name = 'Somebody';
59+
console.log(person, person2);
60+
61+
function cloneObject(obj) {
62+
if (obj === null || typeof obj !== 'object') return obj;
63+
64+
let tmp = obj.constructor();
65+
for (let key in obj) {
66+
tmp[key] = cloneObject(obj[key]);
67+
}
68+
69+
return tmp;
70+
}
71+
72+
const person3 = cloneObject(person);
73+
person3.name = 'New man';
74+
console.log(person, person3);
4875

4976
</script>
5077

0 commit comments

Comments
 (0)