Is there a way to achieve the code bellow with Javascript (ES6)?
If yes, how can I do it? I try this example, but it didn't work.
const funcA = (callback, arg1) => {
console.log("Print arg1: " + arg1); /* Print arg1: argument1 */
let x = 0;
x = callback(x, );
return x;
}
const funcB = (x, prefix) => {
console.log("Print prefix: " + prefix); /* Print prefix: PREFIX_ */
x = x + 1;
return x;
}
/* Exec function funcA */
let x = funcA(funcB( ,"PREFIX_"), "argument1");
console.log("Value of x: " + x); /* Value of x: 1 */
Partial application is not yet possible in js. You need another arrow function that acts as a callback:
To call that you don't need that extra comma:
Somewhen this proposal might allow to write this:
One approach would be to define a default parameter that is a function for the first parameter passed to
funcA
and usevar
to definex
whenfuncA
is calledThis is an approach with a defined placeholder as symbol to identify the parameter which is not yet set.
It features a
this
object which is bind to the calling function for further check and evaluation.If the combined array of
arguments
object andthis.arg
has no moreplaceholder
items, the function is called with parameters and return the function call.If not, the new arguments array is bind to the function and returnd.
(Of course it could be a bit shorter and delegated to another function, but it's a proof of concept.)