Skip to content

Instantly share code, notes, and snippets.

@lailo
Last active June 2, 2017 01:53
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 lailo/2ad7a3bc8826a35d840d3247da088697 to your computer and use it in GitHub Desktop.
Save lailo/2ad7a3bc8826a35d840d3247da088697 to your computer and use it in GitHub Desktop.
Require function param with ES6 object destruction
function isRequired(param) {
throw new Error(`Missing param "${param}" is required.`)
}
function myFunc(
{
optionalParam,
paramWithDefaultVaue = 'my default value',
requiredParam = isRequired('requiredParam'),
} = {}
) {
console.log('Executed "myFunc" successfully.')
}
myFunc()
// Output: Error: Missing param "requiredParam" is required
myFunc({optionalParam: 'bla'})
// Output: Error: Missing param "requiredParam" is required
myFunc({requiredParam: 42})
// Output: Executed myFunc successfully
myFunc({optionalParam: 'bla', requiredParam: 42})
// Output: Executed myFunc successfully
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment