2-1重载:就是根据参数的不同,动态决定调用哪个方法
js中没有重载的概念,因为重复声明,下面的会覆盖上面的
//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)
2-2 函数不定参
js传不定参,函数内部有一个arguments对象,接收传递过来的参数
arguments对象是一个类数组对象
function go(a){console.log(arguments);//argumentsconsole.log(a);//10console.log(arguments[1]);//12}go(10,12)
