How can I pre-set arguments in JavaScript function call? (Partial Function Application)
First of all, you need a partial - there is a difference between a partial and a curry - and here is all you need, without a framework:
function partial(func /*, 0..n args */) {
var args = Array.prototype.slice.call(arguments, 1);
return function() {
var allArguments = args.concat(Array.prototype.slice.call(arguments));
return func.apply(this, allArguments);
};
}
Now, using your example, you can do exactly what you are after:
partial(out, "hello")("world");
partial(out, "hello", "world")();
// and here is my own extended example
var sayHelloTo = partial(out, "Hello");
sayHelloTo("World");
sayHelloTo("Alex");
The partial()
function could be used to implement, but is not currying. Here is a quote from a blog post on the difference:
Where partial application takes a function and from it builds a function which takes fewer arguments, currying builds functions which take multiple arguments by composition of functions which each take a single argument.
Hope that helps.
Is curried javascript what you're looking for?
Using Javascript's apply()
, you can modify the function prototype
Function.prototype.pass = function() {
var args = arguments,
func = this;
return function() {
func.apply(this, args);
}
};
You can then call it as out.pass('hello','world')
apply
takes an array for 2nd argument/parameter.
arguments
is property available inside function which contains all parameters in array like structure.
One other common way to do this is to use bind
loadedFunc = func.bind(this, v1, v2, v3);
then
loadedFunc() === this.func(v1,v2,v3);
this kinda suffice, even though little ugly.