Last active
February 19, 2017 13:27
-
-
Save kaplanmaxe/aa8f8f50f03ab862e9abc934e0a13ed0 to your computer and use it in GitHub Desktop.
JS Objects are By Reference by default
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = { name: 'Max', age: 24 }; | |
const b = a; | |
b.name = 'Barbara'; | |
console.log(b); // { name: 'Barbara', age: 24 } | |
console.log(a); // { name: 'Barbara', age: 24 } (a.name was overwritten as well by b.name) | |
// To avoid passing by reference | |
const foo = { name: 'Max', age: 24 }; | |
const bar = Object.assign({}, foo); | |
bar.name = 'Barbara'; | |
console.log(bar); // { name: 'Barbara', age: 24 } | |
console.log(foo); // { name: 'Max', age: 24 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment