

The dark side of Function.prototype.bind - amasad
http://blog.amasad.me/2012/07/02/the-dark-side-of-functionprototyopebind/

======
ricardobeat
This is a simple misconception: .bind() doesn't affect the original function
in any way, it just _returns a new function_ that is bound to the argument. If
you just keep in mind that .bind _creates_ functions, as per the method
documentation, you won't have any trouble:

    
    
        Creates a new function that when called, itself calls
        this function in the context of the provided `this` value [1]
    

In the Editable example, the problem is not even bind()'s fault. Even if you
used the original, unbound functions for the events, replacing them later
wouldn't change the event handlers... what you'd need to respect the prototype
chain is to call the object's method inside the handler:

    
    
        this.$reset.click(function(){
            this.reset()
        }.bind(this))
    
        //or
    
        var self = this
        this.$reset.click(function(){
            self.reset()
        })
    

[1]
[https://developer.mozilla.org/en/JavaScript/Reference/Global...](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind)

~~~
amasad
Not sure if you continued on reading to the part where I propose a bind
function that doesn't break dynamic dispatch.

    
    
        var bind = function (obj, m) {
            return function () {
                obj[m].apply(obj, Array.prototype.slice.call(arguments));
            };
        };

~~~
swah
Have you checked underscore.js' version?

[https://github.com/documentcloud/underscore/blob/master/unde...](https://github.com/documentcloud/underscore/blob/master/underscore.js#L503)

~~~
amasad
Yes underscore's bind works exactly like the Native bind and even uses it if
it exists.

