1

如果我想部分应用我可以使用的函数bind,但似乎我必须影响函数的接收者(的第一个参数bind)。这个对吗?

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

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

1 回答 1

1

部分应用使用bind而不影响接收器

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

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

当然,许多库中也有这样的功能,例如UnderscoreLodashRamda等。但是没有原生的等价物。

于 2015-02-26T17:12:10.927 回答