I have the following use of jQuery UI function:
$('.droppable').droppable({
tolerance: 'touch',
over: function () {
var hasHiddenList = $(this).children('ul.hidden');
if (hasHiddenList.length)
hasHiddenList.removeClass('hidden');
},
out: function () {
var hasEmptyList = $(this).children('ul.empty');
if (hasEmptyList.length)
hasEmptyList.addClass('hidden');
},
drop: function () {
var hasEmptyList = $(this).children('ul.empty');
if (hasEmptyList.length)
hasEmptyList.removeClass('empty');
}
});
And I am wondering if I can define the variables hasHiddenList and hasEmptyList outside the callback functions, since it is the same variable in all of them.
If you really, really want the variables, there are two options.
The obvious one (with pitfalls) is this:
Variables have been extracted, the code is less repetitive. All good.
Now, the problem is that the selectors are just run once. The callback
over,outanddropare called every time one of those events occur, buthasHiddenListandhasEmptyListwill not have been updated; they will still be the same thing between every invocation. Probably not what you want.The way to remedy this is to use functions instead of variables, like this:
This abstracts away how the queries are performed (you only have to write
$(target).children('ul.empty');once instead of twice) but in reality the code has not really become simpler. Actually, I would even say that it’s harder to follow it now. On the other hand, if you were using the same selector even more than twice, lets say five times, then maybe this would actually be useful.Also, note that I’ve removed “has” from the variable names, because “has” makes it sound like they are booleans, which they are not. They are lists of elements.