Explain bindbind() function

孤人 提交于 2019-11-26 16:29:53

问题


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)


回答1:


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) // bindfn

contextbindfn = bindfnanything(context) // fncontext

result = contextbindfnanything(args) // fncontext(args)



来源:https://stackoverflow.com/questions/13504936/explain-bindbind-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!