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

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

问题描述


可能重复:

如何在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()),这样当 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 部分功能的问题/ 321113>这里以及他发布的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);
  };
}


推荐答案

你所追求的是什么部分功能应用

不要被那些不理解它和currying之间的细微差别的人所迷惑,他们 不同。

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

部分函数应用程序可用于实现,但不是 currying。以下是 关于差异的博文 的引用:

Partial function application can be used to implement, but is not currying. Here is a quote from a blog post on the difference:


部分应用程序接受一个函数并从中构建一个函数,该函数接受较少的参数,currying构建函数,这些函数采用多个参数组合每个函数都只有一个参数。

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天全站免登陆