- Are closures too bad for browser's memory?
- Is avoiding closures using bind in JavaScript a good way?
Existing code:
var oControl = new Control({
var self = this;
click: function(e){ //closure that's accessing self var
self.performAction();
}
});
I have been told to avoid writing code as shown above. Reason given - a closure is created in the above code. I have been suggested using bind to overcome the above problem.
Modified code:
var oControl = new Control({
var self = this;
click: function(e){ //Not a closure
this.performAction(); //Is this more readable?
}.bind(self)
});
I am not too sure if the modified code is more readable. Somehow, I feel that this is in a way polluted. Probably, there are better ways to avoid closures? But, is there a real need to avoid them in the first place?