我想调用另一个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
语句但这只比较了值而不是传递给函数的原始参数的名称。任何帮助将不胜感激。
答案 0 :(得分:1)
即使可以,也不应该这样做。只需将要匹配的数组作为另一个参数传递:
function compareArrays(userInput, winningNums, matches, listToMark) {
...
markMatches(listToMark)
}
答案 1 :(得分: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;
}
}