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)
bind() The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
The bind() function binds a unique local name to the socket with descriptor socket. After calling socket(), a descriptor does not have a name associated with it. However, it does belong to a particular address family as specified when socket() is called. The exact format of a name depends on the address family.
We use the Bind() method to call a function with the this value, this keyword refers to the same object which is currently selected . In other words, bind() method allows us to easily set which object will be bound by the this keyword when a function or method is invoked.
Using JavaScript bind() for function binding When you pass a method an object is to another function as a callback, the this is lost. For example: let person = { name: 'John Doe', getName: function() { console.log(this.name); } }; setTimeout(person.getName, 1000);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With