Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get an unbound function from a bound function in JavaScript?

Tags:

I'm getting my head wrapped about currying and other techniques using Function.prototype.bind.
It seems extremely useful to change function scope (i.e., this value) in certain situations.

However it looks like you can't change the scope with bind once you already did so:

function f = obj.method.bind(42);  function g = obj.method.bind('Hi');  function f2 = f.bind('Hi'); // “this” is still 42 

Is it possible to retrieve the original unbound function from a bound function at all?

like image 855
Dan Abramov Avatar asked May 09 '12 19:05

Dan Abramov


People also ask

What is bound function in JavaScript?

JavaScript Function bind() With the bind() method, an object can borrow a method from another object. The example below creates 2 objects (person and member).

What is unbound function?

Now, a function which is not bounded from above or below by a finite limit is called an unbounded function. For example: - x is an unbounded function as it extends from −∞ to ∞. Similarly, tanx defined for all real x except for x∈(2n+1)π2 is an unbounded function.

Should you use BIND in JavaScript?

. bind() is used when you need to pass a callback (e.g. some sort of function reference), but you want the caller to call your function with a specific this value.

What is argument binding in JavaScript?

bind is a method on the prototype of all functions in JavaScript. It allows you to create a new function from an existing function, change the new function's this context, and provide any arguments you want the new function to be called with.


2 Answers

What the bind method basically does is something like (not exactly, because arguments are sliced to exclude the context):

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

So basically it's returning another function which will call itself with the given context and arguments. If you then bind it again, you'll be binding this newly created function, which will be as if bind was implemented like:

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

But because the inner function returned by bind acts as a closure where the original context is the one binded first (self), that one will be the context in with your function will be really executed.

like image 102
Win32 Avatar answered Nov 20 '22 00:11

Win32


I thought it would be useful to illustrate Win32's answer with a picture.

A wrapper generated by bind makes sure your function is called with given context no matter what.
Such wrapper will always ignore its own context.

Given a chain of wrappers, any context but the innermost is lost.
Therefore, there is no way to change the context once it has been set using bind.

Chained bind calls

like image 22
Dan Abramov Avatar answered Nov 19 '22 22:11

Dan Abramov