Last active
June 2, 2017 01:53
-
-
Save lailo/2ad7a3bc8826a35d840d3247da088697 to your computer and use it in GitHub Desktop.
Require function param with ES6 object destruction
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
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