In JavaScript, there is no concept of method overloading, which refers to a method with the same name that defines the number of parameters and arguments of different types, which can then be called on demand. If you need to implement different method bodies according to the number of arguments, the correct approach is to define a method by using the arguments parameter array of the method itself to determine the number of parameters and then handle them separately.
Here is an example of a page JavaScript that has multiple methods with the same name:
1 <script type= "Text/javascript" >2 function jsmethod (arg1, arg2) { 3 }45 function jsmethod (arg1, arg2, Arg3) {6 }78 // always call the page last loaded method with the same name, cannot be distinguished by the number of direct arguments Here will call Jsmethod (Arg1, arg2, ARG3) instead of self-thought Jsmethod (Arg1, arg2) 9 </script>
through analysis, there are 3 conclusions:(1) When the page JavaScript has more than one method of the same name, always call the page last loaded method of the same name, can not be distinguished by the number of direct arguments (2) such as the Jsmethod (1, 2) call case, the number of arguments is less than the number of formal parameters; Arg3 is undefined (3) and if it is a call using Jsmethod (1, 2, 3, 4), the number of arguments is more than the number of formal parameters, and the extra arguments on the call can only be obtained by the arguments parameter array of the method itself.
Page JavaScript has multiple invocation analysis of a method with the same name