[英]Using bind for partial application without affecting 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)));
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.