我可以将变量定义为参数吗?

ily*_*lyo 0 javascript jquery jquery-ui

我有以下使用jQuery UI功能:

$('.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');
    }
});
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以定义变量hasHiddenListhasEmptyList回调函数之外,因为它在所有变量中都是相同的变量.

Jak*_*kob 5

更好的是,你甚至不需要if语句和变量:

$('.droppable').droppable({
    tolerance: 'touch',
    over: function () {
      $(this).children('ul.hidden').removeClass('hidden');
    },
    out: function () {
      $(this).children('ul.empty').addClass('hidden');
    },
    drop: function () {
      $(this).children('ul.empty').removeClass('empty');
    }
});
Run Code Online (Sandbox Code Playgroud)