重载:函数名相同,参数不同 就是根据参数的不同,动态决定调用哪个方法
js中没有重载的概念,因为重复声明,下面的会覆盖上面的
// 使用function声明的方法,本质上是window的方法function go(a){console.log(a);}function go(a,b){console.log(a+b);}==var go =function(a){console.log(a)}var go=function(a,b){console.log(a);console.log(a+b);}go(10)
使用arguments对象模拟重载
伪数组1,具有数组的length属性2,按照索引的方式进行存储的3,它没有真正数组的一些方法 pop() push()等等
<script>function getmax(){var max = arguments[0];for(var i=1;i<arguments.length;i++){if(arguments[i]>max){max = arguments[i];}}return max;}console.log(getmax(1,2,3));console.log(getmax(100,24,31,8,90));console.log(getmax(1,2,33,520,24,520.1));</script>
// js 传递传不定参,函数使用arguments对象来管理函数内部的参数
function go(){
if(arguments.length==1){
console.log(arguments[0]);
}else if(arguments.length==2){
console.log(arguments[0]+arguments[1]);
}
}
go(1)
go(10,20)
