4 ms·
The dark side of Function.prototype.bind
- ricardobeat 14y agoThis 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_Objects/Function/bind https://developer.mozilla.org/en/JavaScript/Reference/Global...
- amasad 14y agoNot 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)); }; };