javascript multiple binds

In javascript, if we bind multiple times, only the first bind takes in the provided this value. That is because the bind function returns a new function which doesn’t have this anymore. Subsequent binds won’t have any effect on the original this.

// NOT the real bind; just an example
Function.prototype.bind = function(ctxt) {
    var fn = this;
    return function bound_fn() {
        return fn.apply(ctxt, arguments);
    };
}
// no more 'this' in my_bound_fn
my_bound_fn = original_fn.bind(obj);

ref:
https://stackoverflow.com/questions/33832384/in-javascript-the-first-bind-determines-who-this-is-binding-it-twice-or-a-s
https://stackoverflow.com/questions/26545549/chaining-bind-calls-in-javascript-unexpected-result

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s