为什么如果缺少剩余参数,该函数不会抛出错误?
showStatistics("Mark Teixeira", "New York Yankees", "1st Base");
这是一个定义的函数:
function showStatistics(name, team, position, average, homeruns, rbi) {
document.write("<p><strong>Name:</strong> " + arguments[0] + "<br />");
document.write("<strong>Team:</strong> " + arguments[1] + "<br />");
if (typeof arguments[2] === "string") {
document.write("<strong>Position:</strong> " + position + "<br />");
}
if (typeof arguments[3] === "number") {
document.write("<strong>Batting Average:</strong> " + average + "<br />");
}
if (typeof arguments[4] === "number") {
document.write("<strong>Home Runs:</strong> " + homeruns + "<br />");
}
if (typeof arguments[5] === "number") {
document.write("<strong>Runs Batted In:</strong> " + rbi + "</p>");
}
}
最佳答案
任何未传递的参数在函数内都显示为未定义
。 JavaScript 中没有方法重载。
关于javascript - JavaScript 中的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572212/