<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js的方法重载</title>
<script type="text/javascript">
// JavaScript不存在方法重载
// 通过arguments数组来模拟方法重载的效果
function add() {
var length = arguments.length;
var result;
switch (length)
{
case 2:
result = arguments[0] + arguments[1];
break;
case 3:
result = arguments[0] + arguments[1] + arguments[2];
break;
default:
result = -1;
break;
}
return result;
}
alert(add(1,2)); // 3
alert(add(1,2,3)); // 6
alert(add(1,2,3,4));// -1
</script>
</head>
<body>
</body>
</html>