JavaScript部分应用函数 - 如何只绑定第二个参数? [英] JavaScript partially applied function - How to bind only the 2nd parameter?

查看:153
本文介绍了JavaScript部分应用函数 - 如何只绑定第二个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我遗漏了一些明显的东西,但我无法弄清楚如何在javascript中绑定函数的特定(第n个)参数。我学到的大部分函数式编程都来自Scala,所以我不确定这在JS中是否可行。

Sorry if I'm missing something obvious, but I can't figure out how to bind a specific (nth) argument of a function in javascript. Most of my functional programming I've learned has been from Scala so I'm not sure this is even possible in JS.

例如,我知道我可以做到下面绑定第一个参数

For example, I understand I can do the below to bind the 1st argument

var add = function (a, b) {
   return a + b;
};

add(1, 3); //returns 4

var addThree = add.bind(null, 3);  //this = null.  a = 3
addThree(4);                        //returns 7

但是如何绑定第二个参数并保留第一个参数。换句话说,我怎么才能绑定到'b'?

从mozilla可以看出 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference / Global_Objects / Function / bind ,参数实际上是嵌套的,这使得它看起来必须按特定的顺序排列? (我很可能读错了)

From what i can tell from mozilla - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind , the arguments are actually nested which makes it look like it has to be in specific order? (I'm very possibly reading this wrong)

编辑:
我意识到这是一个人为的例子。我只是想学习以防万一我最终处理比添加2个数字更复杂的事情。我也试图理解bind()参数实际上是如何工作的。

I realize this is a sort of contrived example. I'm just trying to learn in case I eventually deal with something more complex than adding 2 numbers. I'm also trying to understand how the bind() arguments are actually working under the hood.

推荐答案

当然你可以做它。这是使用扩展运算符( ... )的ES6解决方案,因为它更紧凑。

Of course you can do it. Here's an ES6 solution using the spread operator (...), since it's a bit more compact.

// Bind arguments starting after however many are passed in.
function bind_trailing_args(fn, ...bound_args) {
    return function(...args) {
        return fn(...args, ...bound_args);
    };
}

如果您更愿意指定绑定开始的位置:

If you'd prefer to specify the position at which binding starts:

// Bind arguments starting with argument number "n".
function bind_args_from_n(fn, n, ...bound_args) {
    return function(...args) {
        return fn(...args.slice(0, n-1), ...bound_args);
    };
}

在ES5中,你必须捣乱构建参数列表。

IN ES5, you have to muck around with constructing argument lists.

// ES5 version: construct arguments lists yourself
function bind_trailing_args(fn) {
    var bound_args = [].slice.call(arguments, 1);
    return function() {
        var args = [].concat.call(arguments, bound_args);
        return fn.apply(this, args);
    };
}

与前两个例子不同,这个例子处理这个正确。

Unlike the first two examples, this one handles this properly.

在您的示例中:

var addThree = bind_trailing_args(add, 3);
addThree(1) // calls add(1, 3)

您还可以考虑使用JS可用的函数编程库之一,例如 http://osteele.com/sources/javascript/functional/。你想要的东西叫做 rcurry 那里。

You could also consider using one of the functional programming libraries available for JS, such as http://osteele.com/sources/javascript/functional/. The thing you want is called rcurry there.

这篇关于JavaScript部分应用函数 - 如何只绑定第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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