Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain bindbind() function

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)

like image 765
Nik Avatar asked Nov 22 '12 02:11

Nik


People also ask

What is bind () method in JavaScript?

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.

What does bind () do in C?

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.

Why do we use the bind method?

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.

What is bind in JavaScript example?

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);


1 Answers

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)

like image 153
Bergi Avatar answered Oct 18 '22 09:10

Bergi