Page JavaScript has multiple invocation analysis of a method with the same name

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.