How to pass a link to a function with parameters?

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 () { //Some logic here... }; var fr = f; //Here I am passing a reference to function 'f', without parameters fr(); //the 'f' function is invoked, without parameters 

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 () { //Some logic here... }; var fr = function (pars) { f(pars); }; //Here I am creating an anonymous function, and invoking f inside it fr({p : 'a parameter'}); //Invoking the fr function, that will later invoke the f function with parameters 

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); }; } 
+84
javascript pass-by-reference anonymous-function
Dec 16 '08 at 23:30
source share
3 answers

What you need is called a partial function application .

Do not be fooled by those who do not understand the subtle difference between this and curry, they are different.

A partial function application may be used for implementation, but is not executed. Here is a quote from a blog post about the difference :

If a partial application takes a function and builds a function from it that takes fewer arguments, currying builds functions that take several arguments according to the composition of the functions, each of which takes one argument.

This has already been answered by this question for the answer: How can I pre-set arguments in a JavaScript function call?

Example:

 var fr = partial(f, 1, 2, 3); // now, when you invoke fr() it will invoke f(1,2,3) fr(); 

Let's look at this question again for details.

+76
Dec 16 '08 at 23:53
source share

You can also overload the function prototype:

 // partially applies the specified arguments to a function, returning a new function Function.prototype.curry = function( ) { var func = this; var slice = Array.prototype.slice; var appliedArgs = slice.call( arguments, 0 ); return function( ) { var leftoverArgs = slice.call( arguments, 0 ); return func.apply( this, appliedArgs.concat( leftoverArgs ) ); }; }; // can do other fancy things: // flips the first two arguments of a function Function.prototype.flip = function( ) { var func = this; return function( ) { var first = arguments[0]; var second = arguments[1]; var rest = Array.prototype.slice.call( arguments, 2 ); var newArgs = [second, first].concat( rest ); return func.apply( this, newArgs ); }; }; /* eg var foo = function( a, b, c, d ) { console.log( a, b, c, d ); } var iAmA = foo.curry( "I", "am", "a" ); iAmA( "Donkey" ); -> I am a Donkey var bah = foo.flip( ); bah( 1, 2, 3, 4 ); -> 2 1 3 4 */ 
+2
Jan 07 2018-12-12T00:
source share

The following is equivalent to your second code:

 var f = function () { //Some logic here... }; var fr = f; fr(pars); 

If you want to pass a function reference to some other function, you can do something like this:

 function fiz(x, y, z) { return x + y + z; } // elsewhere... function foo(fn, p, q, r) { return function () { return fn(p, q, r); } } // finally... f = foo(fiz, 1, 2, 3); f(); // returns 6 

You are almost certainly better off using the framework for these kinds of things.

-four
Dec 17 '08 at 0:07
source share



All Articles