Possible duplicate:
How can I pre-set arguments in a JavaScript function call? (Partial Function Application)
I need to pass a link to a function with a given set of parameters .
Here is an example of passing reference parameters without :
var f = function () {
Now I need to pass the same function f , but this time I will need to pass the parameters to the link. Now I can do this with an anonymous function and call the function f with parameters inside the newly created function, for example:
var f = function () {
But my question is: Is there a way to pass a direct link to a function f with parameters to fr , but without including it in an anonymous function?
What do I need to assign fr to make it invokable without parameters ( fr() ), so that f (1,2,3) is executed when fr called?
[UPDATE] I followed Jason Bunting on here about the partial function and the JavaScript function that he publishes is exactly what I was looking for. Here is the solution:
function partial(func /*, 0..n args */) { var args = Array.prototype.slice.call(arguments).splice(1); return function() { var allArguments = args.concat(Array.prototype.slice.call(arguments)); return func.apply(this, allArguments); }; }
javascript pass-by-reference anonymous-function
Andreas Grech Dec 16 '08 at 23:30 2008-12-16 23:30
source share