函数参数
2025年4月30日小于 1 分钟
示例代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>函数的参数</title>
</head>
<body>
<script type="text/javascript">
//第一种
function sum(a, b, c)
{
console.log(a+b+c);
}
sum(10,20,30);
//缺少参数 另一个为undefined 结果为NaN
sum(10,20);
//第二种
function add(a,b)
{
console.log(a+b);
}
add(10,20,30);
//第三种 默认值
function jia(a,b,c=30)
{
console.log(a+b+c);
}
jia(10,20);
//第四种
function he()
{
var total=0;
for (var i = 0; i < arguments.length; i++)
{
total+=arguments[i];
};
console.log(total);
}
he(1,2,3,4,5,6,7,8,9);
</script>
</body>
</html>