重载:根据传入的参数不同,动态决定调用那种方法
<script>
function a(num1){
console.log(num1)
}
function a(num1,num2){
console.log(num1+num2);
}
a(1+2); //3
a(1); //NaN
</script>
js不支持重载,要想支持重载,可以使用arguments对象
<script>
function a() {
if (arguments.length == 1) {
console.log(arguments[0])
} else if (arguments.length == 2) {
console.log(arguments[0] + arguments[1])
}
}
a(1, 2); //3
a(1); //1;
</script>