Use of the arguments object in the function

Source: Internet
Author: User

Any function contains an arguments object, which is similar to an array. You can use a subscript to access data members of arguments.

Arguments is mainly used to save the actual parameter data of the called function.

For example:
Function Test (){
Alert (arguments [0]); // 1
Alert (arguments [1]); // 2
}
Test (1, 2, 3 );

The parameter list of the function is also the real parameter data that is stored and passed to the function. arguments and the data of the parameter are synchronized.
The arguments object and the form parameter do not point to the same memory space, but different memory space, and the stored data is synchronized.
Modifying arguments [N] is equivalent to modifying the value of the nth form parameter (assuming that it starts from 0.

For example:
Function Test (a, B, c ){
Arguments [1] = 1;
Alert (B); // 1
Alert (arguments [1] = B); // true

B = 3;
Alert (arguments [1]); // 3
Alert (arguments [1] = B); // true
}
Test (1, 2, 3 );

Special
If the real parameters passed to the function are less than the actual parameters, the arguments and uninitialized parameters will not be associated for synchronization.
Function Test (a, B, c ){
Arguments [2] = 1;
Alert (c); // undefined
Alert (C = arguments [2]); // false

C = 3;
Alert (c); // 3
Alert (arguments [2]); // 1
}
Test (1, 2 );

Attributes of the arguments object
Length: the actual number of parameters passed to the function.
Callee: the function that points to the current arguments.

For example:
Function Test (){
Alert (arguments. callee = test); // true
}
Test ();

Arguments is a javascript keyword and cannot be used as a variable or function identifier.

Function overload
In manyProgramming LanguageContains this feature, defining multiple overload functions with different parameter lists of the same name. However, JavaScript cannot define multiple functions with the same name at the same time,
The function defined later will overwrite the previous function definition with the same name. Javascript uses arguments and the determination of the real parameter type to overload the function.
Function Test (){
If (typeof arguments [0] = 'number '){
Alert ("this is a number ");
} Else if (typeof arguments [0] = 'string '){
Alert ("this is a string ")
} Else {
Alert ("oops ");
}
}

Test (1); // This is a number
Test ("JavaScript"); // This is a string
Test (true); // oops

Function Recursion
Function recursion is programmingAlgorithmA good weapon that can be usedProgramThe structure is simplified. Use arguments. callee to implement function recursion.
The Fibonacci series is a typical recursive case.
F (0) = 0;
F (1) = 1;
F (n) = f (n-1) + f (n-2)
Recursive Implementation Method 1:
Function Fibonacci (n ){
If (n = 0 | n = 1) return N;
Return maid (n-1) + maid (n-2 );
}
Disadvantage: after using the function name directly, if you change the function name, you need to maintain it in three places, with poor maintainability.
Recursive Implementation Method 2:
Function Fibonacci (n ){
If (n = 0 | n = 1) return N;
Return arguments. callee (n-1) + arguments. callee (n-2 );
}
Use arguments. callee to replace the function name, and use the function body and function name to perform the maximum solution.

From: http://www.cnblogs.com/Lbeta/archive/2012/06/04/2534512.html

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.