如果我想部分应用我可以使用的函数bind
,但似乎我必须影响函数的接收者(的第一个参数bind
)。这个对吗?
bind
我想在不影响接收器的情况下 执行部分应用程序。
myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver
如果我想部分应用我可以使用的函数bind
,但似乎我必须影响函数的接收者(的第一个参数bind
)。这个对吗?
bind
我想在不影响接收器的情况下 执行部分应用程序。
myFunction.bind(iDontWantThis, arg1); // I dont want to affect the receiver
部分应用使用
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)));
};
};
当然,许多库中也有这样的功能,例如Underscore、Lodash、Ramda等。但是没有原生的等价物。