<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="css/bootstrap.css" rel="stylesheet">
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script type="text/javascript">
window.onload = function(){
var btn = document.getElementById('btn')
btn.onclick=alert(sum(1,2,3,4));
function sum(){
var result = 0;
for (var i = 0;i < arguments.length;i++){
result+=arguments[i];
}
return result;
}
}
</script>
</head>
<body>
<div class="container">
<h1 class='page-header text-center'>arguments例子</h1>
<div class="row">
<div class='col-md-4 col-md-offset-4'>
<button id='btn' class='btn btn-default'>求和</button>
</div>
</div>
</div>
</body>
</html>
用arguments数组实现不定参数。在函数的定义处可以不给定参数列表,函数内用arguments替代。在函数实例化时再对参数进行任意个数的赋值。