如何使用参数传递对函数的引用? [英] How can I pass a reference to a function, with parameters?

查看:26
本文介绍了如何使用参数传递对函数的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
我如何预设JavaScript 函数调用中的参数?(部分函数应用)

我需要能够使用一组给定的参数传递对函数的引用.

下面是一个不带参数传递引用的例子:

Here is an example of passing a reference without parameters:

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 函数,如下所示:

Now what I need to do is pass the same f function, but this time I would need to pass parameters to the reference. Now, I can do it with an anonymous function and invoke the f function with parameters inside the newly created function, like such:

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

但我的问题是,有没有办法将带有参数的 f 函数的直接引用传递给 fr,但不将其包含在匿名中功能?

But my question is, Is there a way to pass a direct reference to the f function With parameters to fr, but without enclosing it in an anonymous function?

我需要为 fr 分配什么才能使其无需参数即可调用(fr()),以便 f(1,2,3)fr 被调用时执行?

What do I need to assign to fr to make it invokable without parameters (fr()), so that f(1,2,3) is executed when fr is invoked?

[更新]我遵循了 Jason Bunting 的回答这里关于部分函数和他在那里发布的 JavaScript 函数正是我正在寻找的.解决办法如下:

[UPDATE] I followed Jason Bunting's answer to here about the Partial Function and the JavaScript function he posts there 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);
  };
}

推荐答案

你所追求的是偏函数应用.

不要被那些不了解这与咖喱之间细微差别的人所迷惑,它们是不同的.

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.

这已经得到了回答,请参阅此问题以获得答案:如何在 JavaScript 函数调用中预设参数?

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

示例:

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.

这篇关于如何使用参数传递对函数的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆