I find myself writing various functions that contain event handlers. It is advisable to declare the variables required by the handler functions at the root of the parent function (closure), especially if they are jQuery selections, constants required by more than one handler, or some preliminary calculations that I do not need to repeat every time the event fires. A simple example:
var touchDrag = function() {
var x, y, i;
var $mySelection = $('.selection');
$('#some-elem').on( 'touchmove', function(e) {
x = something;
y = something;
i++;
$mySelection.doSomething();
});
}
However, I often see handler variables declared inside handler functions (local). Answering a question about several encoders, some debate ensued, but did not give a clear answer.
, , . , .scroll() touchmove, , , ( var ), , ?