Created
September 8, 2019 11:53
-
-
Save nicc777/ed7d98d1952688be6307d881b5312747 to your computer and use it in GitHub Desktop.
JavaScript - Object reference vs copy
This file contains 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 person = { | |
name: 'Pete' | |
}; | |
const newPerson1 = person; // person is referenced (not a copy) | |
const newPerson2 = { | |
...person // perons is spread (copied) and the reference is not used | |
}; | |
person.name = 'Jan'; | |
console.log(person); // Jan | |
console.log(newPerson1); // Jan - because newPerson1 uses a pointer to person | |
console.log(newPerson2); // Pete - because the original value was copied into newPerson2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the Udemy class, React - The Complete Guide (incl Hooks, React Router, Redux)