I have a rather large set of javascript functions that I am working on refactoring into a set of classes using Prototype.
I was wondering if there was a way I could make binding anonymous functions to the class simpler? I keep forgetting to add the bind this at the end. Or is this just the way it is done all the time?
var arr = this.getSomeArray();
arr.each(function(t) {
t.update(val);
this.updateJSValue(t);
}.bind(this));
Your options are basically to call some function (
bind,addMethodsor another function you write) or use a local variable rather thanthis:If you’ve a large number of functions, the local variable requires the least typing For just a few functions, there isn’t too much difference.