Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tad-lispy/8747b7a7d93c659b613bf469ca9192f3 to your computer and use it in GitHub Desktop.
Save tad-lispy/8747b7a7d93c659b613bf469ca9192f3 to your computer and use it in GitHub Desktop.
Default values with object / array destructuring in CoffeeScript
# 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