Arguments object usage in JavaScript

Source: Internet
Author: User

The arguments object is not an array and accesses a single argument in the same way that it accesses an array element. Index n is actually one of the parameters of the 0...N property of the arguments object.


In JavaScript, you do not need to explicitly indicate the parameter names to access them. Such as:

The code is as follows Copy Code

function Hi () {
if (arguments[0]== "Andy") {
Return
}
Alert (arguments[0]);
}

With Arguments[0] You can access the first parameter, so on.

Using the arguments object can be overloaded, using arguments.length to get the number of parameters of a function, as follows:

The code is as follows Copy Code

function Hi () {
if (arguments.length==1) {
Alert (arguments[0]);
}
else if (arguments.length==2) {
Alert (Arguments[0] + arguments[1]);
}
}


Length property of arguments

Meaning
Returns the number of actual arguments that the caller passed to the function.
Usage
[function.] Arguments.length
The option function parameter is the name of the function object that is currently executing.
Description
When a function object starts executing, the script engine initializes the length property of the arguments object to the number of actual arguments passed to the function.
JS will not take the initiative for you to determine how many parameters you have passed to the function, if you pass more, the spare part is not used, if you less pass, then the parameter value is undefined

So we can use the arguments length property to detect if the correct number of actual arguments are used when calling a function, because JavaScript doesn't do it for you.

Arguments's 0...N properties
Meaning
Returns the actual value of each parameter in a arguments object, which is returned by the arguments property of a function that is executing.
Usage
[function.] arguments[[0|1|2|...| N]]
Parameters
function
Options available. The name of the Function object that is currently executing.
0, 1, 2, ..., n
Required option. A non-negative integer in the range 0 through N, where 0 represents the first argument and N represents the last argument. The value of the last parameter n is arguments.length-1
Description
0. The value returned by the. N property is the actual value passed to the function being executed. Although it is not actually an array of parameters, you can access the parameters that make up the arguments object in the same way that you access the array elements.
Example
The following example demonstrates the use of the 0 ... n attribute of the arguments object.

The following example demonstrates the use of the length property of a arguments object. To fully understand the example, pass more arguments to the function:

The code is as follows Copy Code
function Argtest (A, b) {
var i, S = "The Argtest function expected";
var Numargs = arguments.length;
var Expargs = argtest.length;
if (Expargs < 2)
S + + Expargs + "argument."
Else
S + + Expargs + "arguments."
if (Numargs < 2)
S + + Numargs + "was passed."
Else
S + + Numargs + "were passed."
return (s);
}

callee Properties of arguments

Meaning
represents a reference to the function object itself, which is the body of the specified function object, which facilitates the recursive implementation of the Nameless function or the encapsulation of the function.
Usage
[function.] Arguments.callee the
Optional function argument is the name of the function object that is currently executing. The
Description
Callee property is a member of the arguments object and is available only if the related function is executing. The initial value of the
Callee property is the Function object that is being executed. This allows anonymous recursive functions.
Instance:
calculates the sum of the natural numbers of 1 to n by recursion:

The code is as follows Copy Code

<script>
var sum=function (n) {
if (1==n) {
return 1;
} else {
return n + arguments.callee (n-1);
}
}
Alert (sum (100));
</script>

Add

The code is as follows Copy Code

<pre><script type= "Text/javascript" >
JS arguments object staging function parameters and attributes
In JavaScript, when a function is called, JS creates a arguments object for the function, and the local variable arguments automatically initializes to refer to the object.
The main function of the JS arguments object is to provide a way to determine the number of arguments passed to a function and to refer to unnamed parameters.
JS arguments object only in function body definition, JS arguments object is defined as the form of a number of groups, can be used Arguments[i] reference function parameters, Arguments[0 is the function of the 1th argument, arguments.length = number of parameters , but the JS arguments object is not an array.
JS Arguments Object properties:
(1) Arguments.callee = reference to the function being executed
(2) Arguments.length = number of parameters
application Example:

var FAC = function (x) {
//
Arguments.length = number of arguments when the function is called. (argument)
Arguments.callee.length = number of arguments when the function is defined. (Formal parameters)
//
if (arguments.length = = arguments.callee.length) {
var x = parseint (arguments[0]);
if (x < 2) {
return 1;
}else{
return x * Arguments.callee (X-1);
}
}else{
return 0;
}
}

 document.writeln ("FAC (1) =" + FAC (1));//FAC (1) = 1
 document.writeln ("FAC (2) =" + FAC (2));//FAC (2) = 2
 document.writeln ("FAC (3) =" + FAC ("3"))//FAC (3) = 6
 document.writeln ("FAC (10,11) =" + FAC (10,11 )//FAC (10,11) = 0
 document.writeln ("FAC =" + FAC (")")//FAC = 2432902008176640000
 
</script>

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.