Created
January 7, 2019 14:13
-
-
Save hollygood/4343dece7f29d3f5e22aaf370e47b560 to your computer and use it in GitHub Desktop.
Function parameters with object destructuring
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
// 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