如何使用参数传递对函数的引用?

可能的复制品:
如何在 JavaScript 函数调用中预设参数? (部分函数应用程序)

我需要能够 将引用传递给具有给定参数集的函数

下面是一个传递引用 < em > 没有参数 的示例:

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

现在我需要做的是传递相同的 f函数,但是这次我需要将参数传递给引用。现在,我可以使用一个匿名函数来完成这项工作,并使用新创建的函数中的参数来调用 f函数,比如:

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

但我的问题是 有没有一种方法可以直接将 ABC0函数的引用 With 参数传递给 fr,而不用将其封装在匿名函数中?

我需要分配什么给 fr,使它在没有参数(fr())的情况下可调用,以便在调用 fr时执行 f (1,2,3) ?

我跟踪了 Jason Bunting给你关于 部分函数的回答,他在那里发布的 JavaScript 函数正是我想要的。解决办法如下:

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);
};
}
117956 次浏览

What you are after is called partial function application.

Don't be fooled by those that don't understand the subtle difference between that and currying, they are different.

Partial function application can 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.

This has already been answered, see this question for your answer: How can I pre-set arguments in JavaScript function call?

Example:

var fr = partial(f, 1, 2, 3);


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

Again, see that question for the details.

The following is equivalent to your second code block:

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


var fr = f;


fr(pars);

If you want to actually pass a reference to a function 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're almost certainly better off using a framework for this sort of thing, though.

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 );
};
};


/*
e.g.


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
*/