Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34d56bb

Browse files
committed
completed wesbos#14 Object and Arrays - Reference vs Copy
1 parent f1551ec commit 34d56bb

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@
106106
console.clear();
107107
console.log(wes);
108108

109+
const dev = Object.assign({}, wes);
110+
const dev2 = JSON.parse(JSON.stringify(wes));
111+
//we successfully now made a copy of the wes object, but shallow one level using Object.assign - further level down is not a copy but a reference - such that if you assign dev.social.twitter to '@coolman', wes.social.twitter becomes also '@coolman'
112+
113+
//to do a real clone, use clone deep but also
114+
// you can use parse and stringify
115+
116+
109117
</script>
110118

111119
</body>

0 commit comments

Comments
 (0)