Skip to content

Instantly share code, notes, and snippets.

@paul-jean
Created October 13, 2014 22:22
Show Gist options
  • Select an option

  • Save paul-jean/ac6b212728045ad644db to your computer and use it in GitHub Desktop.

Select an option

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
// 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