解释bindbind()函数 [英] Explain bindbind() function
问题描述
var bindbind = Function.prototype.bind.bind(Function.prototype.bind) ;
我了解它产生的结果:
var bindedContextFunc = bindbind(function)(context);
bindedContextFunc(args);
但是不明白创建这个函数的过程,我的意思是 bind OK。
我们在这里有三次 Function.prototype.bind
函数,它们的(简化)代码
function bind(context){
var fn = this;
return function(){
return fn.apply(context,arguments);
}
}
我会缩写为一种更实用的风格,部分应用:绑定 fn (context) - > fn context 。
那么它有什么作用?你有 bind.call(bind,bind)
或绑定 bind (bind)。让我们展开这个来绑定 bind 。如果我们现在提供了一些参数?
bind bind (bind)(fn) (上下文)
绑定绑定 (fn)(上下文)
bind fn (context)
fn context
我们可以将它分配给一些变量,以使结果更清晰:
bindbind = bind bind (bind)
bindfn = bindbind anything (fn)
//
bind < sub>
contextbindfn = bindfn anything (context)
//
fn context
result = contextbindfn anything (args)
//
fn context (args)
Can someone explain this function?
var bindbind = Function.prototype.bind.bind(Function.prototype.bind);
I understand the result it produce:
var bindedContextFunc = bindbind(function)(context);
bindedContextFunc(args);
But do not understand process of creating this functions, I mean part bind(Function.prototype.bind)
OK. We have three times the Function.prototype.bind
function here, whose (simplified) code
function bind(context) {
var fn = this;
return function() {
return fn.apply(context, arguments);
}
}
I will abbreviate in a more functional style with lots of partial application: bindfn(context) -> fncontext.
So what does it do? You have got bind.call(bind, bind)
or bindbind(bind). Let's expand this to bindbind. What if we now supplied some arguments to it?
bindbind(bind) (fn) (context)
bindbind(fn) (context)
bindfn(context)
fncontext
Here we are. We can assign this to some variables to make the result clearer:
bindbind = bindbind(bind)
bindfn = bindbindanything(fn)
//
bindfncontextbindfn = bindfnanything(context)
//
fncontextresult = contextbindfnanything(args)
//
fncontext(args)
这篇关于解释bindbind()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!