How to implement the overloading of JS function

Source: Internet
Author: User

JavaScript cannot support overloading of functions, as follows:

  
 
  1. function F(length )
  2. {
  3. Alert ("High is:"+length);
  4. }
  5. function F(length,width )
  6. {
  7. Alert ("High is:"+length+", Width:"+width );
  8. }

The above code does not work, because there is no relationship between the number of arguments in the function definition and the number of arguments in the function call. You can use F.arguments[0] and f.arguments[1 in the function to get the first and second arguments that are passed in when called, so it is no problem to define function (length), followed by the F (10,10) call. So in the above code, the second function is never going to be called, so how can you implement functions like function overloading?

That is to judge the number of arguments passed in the call with f.arguments.length in the function definition. Then different processing methods are used for different situations.
As follows:

  
 
  1. function F()
  2. {
  3. var Len = arguments. length ;
  4. if (1 = = len )
  5. {
  6. var length = arguments [0];
  7. var width = arguments [1];
  8. F2 (length,width);
  9. }
  10. Else
  11. {
  12. var length = arguments [0];
  13. F1 (length);
  14. }
  15. }
  16. function F1(length )
  17. {
  18. Alert ("High is:"+length);
  19. }
  20. function F2(length,width )
  21. {
  22. Alert ("High is:"+length+", Width:"+width );
  23. }

In this way, you can pass in a parameter to the function f () or pass in two parameters, such as f (10) and F (10,10);

Personally, although this can be implemented overload, but it is not very good, we can implement the overload in a function according to the situation, if the two functions to be overloaded with a large difference, then retain two functions, and if the implementation of two functions basically similar, then you can judge in a function, processing different parts, Instead of having to write three functions like the one above, the following:

  
 
  1. function F(length )
  2. {
  3. var Len = arguments. length ;
  4. if (1 = = len )
  5. {
  6. var width = arguments [1];
  7. Alert ("High is:"+length+", Width:"+width );
  8. }
  9. Else
  10. {
  11. Alert ("High is:"+length);
  12. }
  13. }

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.