重载:就是根据参数的不同,动态决定调用哪个方法
    js中没有重载的概念,因为重复声明,下面的会覆盖上面的
    使用function声明的方法,本质上是window的方法

    1. -<script>
    2. /* 使用function声明的方法,本质上是window的方法 */
    3. function go(a){
    4. console.log(a)
    5. }
    6. function go(a,b){
    7. console.log(a);
    8. console.log(a+b);
    9. }
    10. window.go(10);
    11. go(10,20)
    12. </script>

    重载:函数名相同,参数不同,根据传入的参数动态决定调用那个方法 JS不支持重载
    就要JS支持 arguments对象
    JS传不定参,函数使用arguments对象管理函数内部的参数

    1. function go(){
    2. console.log(arguments)
    3. }
    4. go(10,23,4)
    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)
    1. <script>
    2. /*
    3. 一个参数
    4. 二个参数
    5. */
    6. function go(){
    7. if(arguments.length ==1){
    8. console.log(arguments[0])
    9. }else if(arguments.length==2){
    10. console.log(arguments[0]);
    11. console.log(arguments[0]+arguments[1])
    12. }
    13. }
    14. // go(10)
    15. go(10,20)
    16. </script>