Example of using javascript arguments: arguments

Source: Internet
Author: User

Example of using javascript arguments: arguments

Copy codeThe Code is as follows:
<Script Language = "JavaScript">
// The first parameter value.
Function test (a, B, c, d ){
Alert (arguments [0]);
}
// Arguments [0] is actually a. Similarly, arguments [1] is B, followed by c and d.
</Script>

 
Copy codeThe Code is as follows:
<Script Language = "JavaScript">
{
Function function_Name (exp1, exp2, exp3, exp4)
{
Var umber = "";
Umber = arguments. length;
Alert (umber );
}
Function_Name ('A', 'B', 'C', 'D ');
// Function_Name ("a", "B", "c", "d"); can be called
}
</Script>

1. In JavaScript, the arguments object is a special object, which is actually a built-in attribute of the current function. Arguments is very similar to Array, but it is not actually an Array instance. The following code can be used to prove the situation (in fact, in the funcArg function, calling arguments does not need to be written as funcArg. arguments, but can be directly written as arguments ).

Copy codeThe Code is as follows:
Array. prototype. testArg = "test ";
Function funcArg (){
Alert (funcArg. arguments. testArg );
Alert (funcArg. arguments [0]);
}
Alert (new Array (). testArg); // result: "test"
FuncArg (10); // result: "undefined" "10"

2. The length of an arguments object is determined by the number of arguments rather than the number of arguments. Parameters are the variables used to re-store the memory space in the function, but they do not overlap with the memory space of the arguments object. When both arguments and Values exist, the values are synchronized. However, if one of them has no values, the values without values cannot be synchronized. The following code can be verified.

Copy codeThe Code is as follows:
Function f (a, B, c ){
Alert (arguments. length); // result: "2"
A = 100;
Alert (arguments [0]); // results: "100"
Arguments [0] = "qqyumidi ";
Alert (a); // result: "qqyumidi"
Alert (c); // result: "undefined"
C = 2012;
Alert (arguments [2]); // result: "undefined"
}
F (1, 2 );

3. The declaration and calling features of functions in JavaScript show that functions in JavaScript cannot be overloaded.

Based on the reloads in other languages: "function return values are different or the number of parameters is different", we can conclude that:

First, the Javascript function declaration does not return value type;

Second, the number of parameters in JavaScript is strictly to facilitate variable operations in the function. In fact, the real parameters are already stored in the arguments object.

In addition, the JavaScript function itself has a deep understanding of why functions in JavaScript cannot be overloaded: In JavaScript, functions are actually objects, and function names are references to functions, or the function name itself is a variable. The function declaration and function expression shown below are the same (without considering the differences between function declaration and function expression ), it is very helpful to understand that functions in JavaScript cannot be overloaded.

Copy codeThe Code is as follows:
Function f (){
Return a + 10;
}
Function f (){
Return a-10;
}
// Without considering the difference between a function declaration and a function expression, it is equivalent to the following:
Var f = function (){
Return a + 10;
}
Var f = function (){
Return a-10;
}

4. The arguments object has a very useful property: callee. Arguments. callee returns the current function reference of the arguments object. We recommend that you replace the function name with arguments. callee when using a recursive function call.

As follows:

Copy codeThe Code is as follows:
Function count (){
If (a = 1 ){
Return 1;
}
Return a + arguments. callee (-- );
}
Var mm = count (10 );
Alert (mm );

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.