Skip to content

Instantly share code, notes, and snippets.

@hollygood
Created January 7, 2019 14:13
Show Gist options
  • Save hollygood/4343dece7f29d3f5e22aaf370e47b560 to your computer and use it in GitHub Desktop.
Save hollygood/4343dece7f29d3f5e22aaf370e47b560 to your computer and use it in GitHub Desktop.
Function parameters with object destructuring
// before
function renderList(list, ordered, color, bgColor) {
// set defaults for optional parameters
if (typeof ordered === undefined) ordered = true
if (typeof color === undefined) color = '#1e2a2d'
if (typeof bgColor === undefined) color = 'transparent'
/* code to render list would go here 😉 */
}
// before
renderList(['one', 'two'], undefined, undefined, '#5ad')
// after
function renderList(list, {
ordered = true,
color = '#1e2a2d',
bgColor = 'transparent'
} = {}) {
/* ... */
}
// after: simple use
renderList(['love', 'patience', 'pain']);
// after: with only one optional argument (bgColor)
renderList(['one', 'two'], { bgColor: '#5ad' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment