Explain bindbind () function

Can someone explain this feature?

var bindbind = Function.prototype.bind.bind(Function.prototype.bind); 

I understand the result that it produces:

 var bindedContextFunc = bindbind(function)(context); bindedContextFunc(args); 

But I don’t understand the process of creating these functions, I mean the bind(Function.prototype.bind) part bind(Function.prototype.bind)

+6
javascript functional-programming
source share
1 answer

OK Here we have three functions Function.prototype.bind , whose (simplified) code

 function bind(context) { var fn = this; return function() { return fn.apply(context, arguments); } } 

I will shrink in a more functional style with a lot of partial application: bind fn (context) -> fn context .

And what is he doing? You have bind.call(bind, bind) or bind bind (bind). Let expand this to bind bind . What if we now provide him with some arguments?

bind bind (bind) (fn) (context)

bind bind (fn) (context)

bindings <sub> psub> (context)

n <sub> contextub>

Here we are. We can assign this to some variables to make the result clearer:

bindbind = bind bind (bind)

bindfn = bindbind anything (fn) // bind fn

contextbindfn = bindfn anything (context) // fn context

result = contextbindfn anything (args) // fn context (args)

+4
source share

All Articles