0

我想调用另一个 jQuery 函数并传递另一个参数 - 我传递的参数取决于传递给原始函数的参数的名称。所以我可能有这样的事情:

matchedNumbers1 = compareArrays(userNumbers, winningNumbers1, matchedNumbers1);
matchedNumbers2 = compareArrays(userNumbers, winningNumbers2, matchedNumbers2);
matchedNumbers2 = compareArrays(userNumbers, winningNumbers3, matchedNumbers2);

//COMPARE INPUTTED ARRAY OF NUMBERS TO WINNING ARRAYS OF NUMBERS
    function compareArrays (userInput, winningNums, matches) {
        matches = 0;
        allMatchedNumbers.length = 0;
        $(userInput).each(function(i) {
            $(winningNums).each(function(j) {
                if (userInput[i] == winningNums[j]) {
                    allMatchedNumbers[matches] = userInput[i];
                    matches++;
                }
            });
        });
        switch (winningNums) {
            case 'winningNumbers1':
                alert("!!!!!");
                markMatches(ListItems1);
                break;
            case 'winningNumbers2':
                markMatches(ListItems2);
                break;
            case 'winningNumbers3':
                markMatches(ListItems3);
                break;
        }
        return matches;
    }

希望上面的代码能清楚地说明我想要做什么。我尝试使用switch语句,但这仅比较值而不是传递给函数的原始参数的名称。任何帮助,将不胜感激。

4

2 回答 2

1

你不应该那样做,即使你可以。只需传入要匹配的数组作为另一个参数:

function compareArrays(userInput, winningNums, matches, listToMark) {
    ...

    markMatches(listToMark)
}
于 2012-11-18T18:08:56.923 回答
0
function winningNumbers1(){
    var result = 1;
    return {result:result, fname: 'winningNumbers1'};
}

function winningNumbers2(){
    var result = 2;
    return {result:result, fname: 'winningNumbers2'};
}

function compareArrays (userInput, winningNums, matches) {
    var result = winningNums ? winningNums() : null;
    if(result)
    switch(result.fname){
        case 'winningNumbers1': 
        // action
            break;
        case 'winningNumbers2': 
        // action
            break;
    }
}
于 2012-11-18T18:11:40.097 回答