JavaScript bind, currying and arrow functions

How many times have you written code like this:
var foo = { makeRequest: function () { $.get(‘/foo’, function (result) { this.update(result); }); ); }, update: function (data) { /* … */ } }; //somewhere later in the code foo.makeRequest(); Only to ha…

How many times have you written code like this: var foo = { makeRequest: function () { $.get('/foo', function (result) { this.update(result); }); ); }, update: function (data) { /* ... */ } }; //somewhere later in the code foo.makeRequest(); Only to have it poo itself saying that this.update is not a function? Maybe it was with an event handler not an AJAX request, all in all it’s the same problem, you tried to use something and JavaScript changed the value of this on you.
Print Share Comment Cite Upload Translate
CITATION GOES HERE CITATION GOES HERE
Select a language: