Created
May 9, 2015 01:01
-
-
Save nathggns/8125de8b679a08fb7a87 to your computer and use it in GitHub Desktop.
Explaining dictionary merges
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
var obj = { | |
child : { | |
a : '0', | |
b : '2' | |
} | |
}; | |
var extra = { | |
b : '3' | |
}; | |
obj.child = { | |
// The ... operator "spreads" a dictionary, and as such merges it into the dictionary currently being defined | |
...( | |
// This evaluates to the existing nested object, or an empty object if on doesn't exist | |
obj.child || {} | |
), | |
...extra | |
}; | |
console.log(obj.child.b); | |
// 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment