是否需要在javascript函数中定义参数?我的问题是关于我的comchoice
功能下面,我简单地使用开放和关闭括号,而不给任何可以改变的参数。是否需要在Javascript函数中定义参数?
我把我的剧本完整的代码仅作参考
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var compchoice = function()
{
if (computerChoice <= 0.34)
{
return computerChoice = "Rock";
}
else if(0.35 <= computerChoice <= 0.67)
{
return computerChoice = "Paper";
}
if (0.68 <= computerChoice <= 1)
{
return computerChoice = "Scissors";
}
};
compchoice();
var compare = function (choice1, choice2)
{
if (choice1 === choice2)
{
return alert("The result is a tie!");
}
if (choice1 === "Rock")
{
if (choice2 === "Scissors")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Paper wins!");
}
}
else if (choice1 === "Scissors")
{
if (choice2 === "Rock")
{
return alert("Rock wins!");
}
else if (choice2 === "Paper")
{
return alert("Schissors wins!");
}
}
};
compare(userChoice, computerChoice);
不,它不是必需的。 –
当然不是。有些函数只是不期望任何参数,所以要求每个函数都有参数是很愚蠢的。另外,你为什么问?你的代码不能按预期工作吗?了解更多关于定义函数的信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function。 –
这取决于你如何定义你的功能。如果你需要在你的函数定义中使用传递的参数(就像你在'compare'函数中做的那样),你可能需要定义这些参数,但这不是javascript为每个函数明确需要的参数。 – tewathia