简体   繁体   English

如何检查哪个参数已传递给函数

[英]How to check which argument has been passed to a function

I'd like to make a call to another jQuery function and pass another argument - the argument I pass depends on the name of the argument that was passed to the original function. 我想调用另一个jQuery函数并传递另一个参数-我传递的参数取决于传递给原始函数的参数的名称。 So i might have something like this: 所以我可能会有这样的事情:

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;
    }

Hopefully the code above makes it clear what i'm trying to do. 希望上面的代码可以清楚说明我要做什么。 I tried using a switch statement but this only compares the value and not the name of the original argument that was passed to the function. 我尝试使用switch语句,但这仅比较值,而不比较传递给函数的原始参数的名称。 Any help would be appreciated. 任何帮助,将不胜感激。

You shouldn't be doing that, even if you could. 即使可以,您也不应该这样做。 Just pass in the array that you want to match against as another argument: 只需将您要匹配的数组作为另一个参数传递即可:

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

    markMatches(listToMark)
}
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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM