Note 4 --- js JavaScript function (2) Overload

 First, the overloaded function

        When the same function name, a function of a plurality of different parameter list, when invoked, calls the appropriate function depending on the selected number of parameters passed, it reduces the number of functions. 

        js overloading syntax is not supported by default, because of the presence of the same name function does not allow multiple js, if there is a function of the same name, when running the program, js statement ahead of a final mechanism will function overwrites all the previous functions, though not js support overloading, but comes with a js function arguments object to achieve the overloaded function, the arguments object without human creation. arguments is an array of class objects, API but is not a true array, and the array is not common, as long as the uncertainty came when the number of parameters, they do not define the parameter list, stored-value all incoming parameters with arguments. See example below,

   function the Add () {// depending on the parameters passed, have reached the cumulative effect
                  var SUM = 0 ;
                for ( var I = 0; I <The arguments.length; I ++ ) { 
                    SUM + = arguments [I]; 

               } 
                Console .log (SUM); 
          } 
          the Add ( . 1); // . 1 
          the Add (1,2); // . 3 
          the Add (l, 2,3); // . 6
          

 

Guess you like

Origin www.cnblogs.com/houcong/p/11417648.html