I have a the following code
this.addHandlers = function (interactions) {
for(var eventType in interactions) {
if(interactions[eventType]) {
this.on(eventType, function () {
// do something with interactions[eventType]
});
}
}
};
jshint gives me the following warning
Don't make functions within a loop
But as I need the eventType variable which is created in the loop to complete the closure within my event handler I can't create the function outside my loop. I could get rid of the warning by moving the call to this.on
into a function, but I feel like this is missing the point.
How could I not make the function in the loop?