重载:就是根据参数的不同,动态决定调用哪个方法
js中没有重载的概念,因为重复声明,下面的会覆盖上面的
使用function声明的方法,本质上是window的方法
-<script>/* 使用function声明的方法,本质上是window的方法 */function go(a){console.log(a)}function go(a,b){console.log(a);console.log(a+b);}window.go(10);go(10,20)</script>
重载:函数名相同,参数不同,根据传入的参数动态决定调用那个方法 JS不支持重载
就要JS支持 arguments对象
JS传不定参,函数使用arguments对象管理函数内部的参数
function go(){console.log(arguments)}go(10,23,4)
//js中没有重载的概念,因为重复声明,下面的会覆盖上面的function go(a){console.log(a);}function go(a,b){console.log(a+b);}
使用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)
<script>/*一个参数二个参数*/function go(){if(arguments.length ==1){console.log(arguments[0])}else if(arguments.length==2){console.log(arguments[0]);console.log(arguments[0]+arguments[1])}}// go(10)go(10,20)</script>
