[英]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.