重载:就是根据参数的不同,动态决定调用哪个方法

js中没有重载的概念,因为重复声明,下面的会覆盖上面的

  1. //js中没有重载的概念,因为重复声明,下面的会覆盖上面的
  2. function go(a){
  3. console.log(a);
  4. }
  5. function go(a,b){
  6. console.log(a+b);
  7. }

使用arguments对象模拟重载

  1. function go(){
  2. if(arguments.length==1){
  3. console.log(arguments[0]);
  4. }else if(arguments.length==2){
  5. console.log(arguments[0]+arguments[1]);
  6. }
  7. }
  8. go(1)
  9. go(10,20)