I am trying to write a JavaScript function that will return its first argument(function) with all the rest of its arguments as preset parameters to that function.
So
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.