使用bind进行部分应用而不影响接收器 [英] Using bind for partial application without affecting the receiver

查看:110
本文介绍了使用bind进行部分应用而不影响接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想部分应用一个函数,我可以使用 bind ,但似乎我必须影响函数的接收者(<$ c $的第一个参数) C>结合)。这是正确的吗?

If I want to partially apply a function I can use bind, but it seems I have to affect the receiver of the function (the first argument to bind). Is this correct?

我想在不影响接收器的情况下使用 bind 执行部分应用程序。

I want to perform partial application using bind without affecting the receiver.

myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver


推荐答案


部分申请使用 bind 而不影响收件人

这是不可能。 bind 被明确设计为部分应用第0个参数 - 值,以及可选的更多参数。如果您只想修复函数的第一个(可能更多)参数,但保留未绑定,则需要使用不同的函数:

That's not possible. bind was explicitly designed to partially apply the "zeroth argument" - the this value, and optionally more arguments. If you only want to fix the first (and potentially more) parameters of your function, but leave this unbound, you'll need to use a different function:

Function.prototype.partial = function() {
    if (arguments.length == 0)
        return this;
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function() {
        return fn.apply(this, args.concat(Array.prototype.slice.call(arguments)));
    };
};

当然,许多图书馆也提供这样的功能,例如: 下划线 Lodash Ramda 等。但是没有原生的等价物。

Of course, such a function is also available in many libraries, e.g. Underscore, Lodash, Ramda etc. There is no native equivalent however.

这篇关于使用bind进行部分应用而不影响接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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