Using bind for partial application without affecting the receiver

强颜欢笑 提交于 2019-12-02 06:40:21

partial application using bind without affecting the receiver

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!