Created
May 20, 2016 08:03
-
-
Save tad-lispy/8747b7a7d93c659b613bf469ca9192f3 to your computer and use it in GitHub Desktop.
Default values with object / array destructuring in CoffeeScript
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
# You can have default values in object destructuring | |
o1 = a: 1, b: 2 | |
{ a, b, c = 3} = o1 | |
c is 3 | |
o2 = a: 1, b: 2, c: 5 | |
{ a, b, c = 3} = o2 | |
c is 5 | |
# Same with array: | |
a1 = [ 1, 2 ] | |
[ a, b, c = 3 ] = a1 | |
c is 3 | |
a2 = [ 1, 2, 5 ] | |
[ a, b, c = 3 ] = a2 | |
c is 5 | |
# Works really nice with options function parameters with destructuring in signature: | |
fn = ({a, b, c = 3}) -> c | |
(fn a: 1, b: 2) is 3 | |
(fn a: 1, b: 2, c: 5) is 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment