This 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 and this.arg
has no more placeholder
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.
[?]
denotes the placeholder symbol
funcB x prefix this.args args action
------- --- --------- ------------- -------------- ------------------------------
1. call [?] "PREFIX_" [?], "PREFIX_" return calling fn w/ bound args
2. call 0 [?] [?], "PREFIX_" 0, "PREFIX_" return fn call with args
3. call 0 "PREFIX_" return 1
(Of course it could be a bit shorter and delegated to another function, but it's a proof of concept.)
function funcA(callback, arg1) {
console.log('funcA', callback, arg1)
return callback(0, placeholder);
}
function funcB(x, prefix) {
var args = this && this.args || [],
temp = Array.from(arguments);
console.log('funcB', isPlaceholder(x) ? '[?]' : x, isPlaceholder(prefix) ? '[?]' : prefix);
// placeholder part
if (temp.some(isPlaceholder)) {
temp.forEach((a, i) => isPlaceholder(a) && i in args || (args[i] = a));
return args.some(isPlaceholder)
? funcB.bind({ args })
: funcB(...args);
}
// origin function body
return x + 1;
}
const
placeholder = Symbol('placeholder'),
isPlaceholder = v => v === placeholder;
console.log("Value of x: " + funcA(funcB(placeholder, "PREFIX_"), "argument1"));