Created
October 13, 2014 22:22
-
-
Save paul-jean/ac6b212728045ad644db to your computer and use it in GitHub Desktop.
Referencing the same child object from two different parent objects means mutations to the common child effect both parents
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
| // Referencing the *same* object on A.X and B.X, means changing A.X also changes B.X: | |
| var A = {}, B = {}; | |
| var commonChild = {a:'sameValue'}; | |
| A.X = commonChild; | |
| B.X = commonChild; | |
| A.X.b = 'alsoSameValue'; | |
| A.X.b === B.X.b // true | |
| // Referencing *different* objects on A.X and B.X, means changing A.X does NOT change B.X: | |
| var A = {}, B = {}; | |
| var child = {a:'value'}; | |
| var differentChild = {a:'differentValue'}; | |
| A.X = child; | |
| B.X = differentChild; | |
| A.X.b = 'onlyForA'; | |
| A.X.b === B.X.b // false | |
| B.X.b === undefined // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment