JavaScript arguments and JavaScript function overloading

Source: Internet
Author: User

1. All functions have a arguments object of their own, which includes the parameters to be called by the letter. He is not an array, and if you use typeof arguments, the return is ' object '. Although we can call arguments with the method of invoking the data. such as length, and the index method. However, the push and pop objects of the array are not applicable.

2. There is no relationship between the number of arguments in the function definition and the number of arguments when the function is called. In the function can use f.arguments[0] and f.arguments[1] to get the first and second arguments passed in the call, arguments can not be created, is the function of its own parameters, only when the function to start execution is to be used.

Although the use of arguments is much like an array, it is not an array. The length of the arguments object is determined by the number of arguments, not the number of parameters. A parameter is a variable inside a function that re-opens the memory space, but it does not overlap with the arguments object memory space.
1 function Argumentstest (A, b) {2     // alert (typeof arguments); 3     alert (arguments.length); 4     Alert (arguments[1]); 5 }6 argumentstest (1,2,3,4);

The result of the output is 4, 2;

1 function Argumentstest (A, b) {2    alert (arguments.callee); 3     alert (arguments.callee.length); 4 }5 argumentstest (1,2,3,4);

The result of the output is argumentstest,2;

3. The Declaration and invocation of functions in JavaScript makes it possible to see that functions in JavaScript cannot be overloaded.

Depending on the overloads in other languages: "The return value of the function is different or the number of formal parameters is different", we can draw the above conclusion:

First: The declaration of a JavaScript function is not a return value type;

Second: The number of formal parameters in JavaScript is strictly in order to facilitate the manipulation of variables in the function, and the actual arguments are already stored in the arguments object.

In addition, the JavaScript function itself is a deep understanding of why a function in JavaScript cannot be overloaded: in JavaScript, a function is actually an object, a function name is a reference to a function, or the function name itself is a variable.

How do you implement the overloading of JavaScript ?

Use arguments to determine the number of arguments using different methods to implement function overloading:

1<script language= "JavaScript" >2 functionf (length)3 {4     varlen=arguments.length;5     if(1 = =len)6     {7         varwidth = arguments[1];8Alert ("High:" +length+ ", Width:" +width);9     }Ten     Else One     { AAlert ("High to:" +length); -     } - } the</srcipt>

There is a very useful property in the 4.arguments object: callee. Arguments.callee returns the current function reference where this arguments object resides. It is recommended to use Arguments.callee instead of the function name itself when using recursive function calls.

function Count (a) {2     if (A==1) {3         return 1; 4     5     return A + arguments.callee (-a); 6 }var mm = count (ten); 9 alert (mm);

JavaScript arguments and JavaScript function overloading

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.