Problem
Destructuring functional arguments are a great feature in ES6. Suppose we want to accept functionwith a name that has a keyfObjecta
function f({ a }) {
return a;
}
We have default values for the case when the parameter was not provided to the function to avoid Type Error
function f({ a } = {}) {
return a;
}
This will help in the following case.
const a = f();
Although, he will fail on
const a = f(null); // TypeError: Cannot match against 'undefined' or 'null'.
You can see how Babel translates the function into ES5 here .
Possible solutions
. Python , JS , .
, checkInputObject, , ( ).
@
const f = checkInputObject(['a'])(({ a }) => a);
: @
@checkInputObject(['a'])
function f({ a }) {
return a;
}
, ( )
function f(param) {
if (!param) {
return;
}
const { a } = param;
return a;
}
, checkInputObject,
const fChecker = checkInputObject(['a']);
function f(param) {
const { a } = fChecker(param);
return a;
}
, . , undefined.
,
function f({a: [, c]) {
return c;
}
undefined f().
- [] ?
: , , , . , .