Home > Article > Web Front-end > How to use arguments in js
arguments feature
arguments objects cannot be created explicitly. The arguments object is only available at the beginning of the function. The arguments object of a function is not an array, and individual arguments are accessed in the same way as array elements. The index n is actually one of the parameters of the 0…n property of the arguments object.
In JavaScript, you can access parameter names without explicitly specifying them. For example: The length attribute of
function hi(){ if(arguments[0]=="andy"){ return; } alert(arguments[0]); }
arguments
means
returns the actual number of parameters passed to the function by the calling program.
Usage
[function.]arguments.length
where the optional function parameter is the name of the Function object currently being executed.
Explanation
When the Function object starts executing, the script engine initializes the length property of the arguments object to the actual number of arguments passed to the function.
js will not actively judge for you how many parameters you have passed to the function. If you pass more parameters, the extra parts will not be used. If you pass less, then the value of the parameters not passed will be undefined
So we can use the length attribute of arguments to detect whether the correct number of actual parameters is used when calling the function, because javascript will not do these things for you
arguments 0...n attribute
meaning
return The actual value of each parameter in an arguments object. The corresponding value is returned by the arguments property of an executing function.
Usage
[function.]arguments[[0|1|2|...|n]]
Parameters
function
Optional. The name of the Function object currently executing.
0, 1, 2, …, n
Required. A nonnegative integer in the range 0 to n, where 0 represents the first argument and n represents the last argument. The value of the last parameter n is arguments.length-1
Explanation
0 . . . The value returned by the n attribute is the actual value passed to the executing function. Although not actually an array of arguments, you can access the individual arguments that make up the arguments object in the same way you access array elements.
Example
The following example demonstrates the usage of the 0 . . . n attribute of the arguments object
function ArgTest(){ var s = ""; s += "The individual arguments are: " for (n=0; n< arguments.length; n++){ s += ArgTest.arguments[n]; s += " "; } return(s); } print(ArgTest(1, 2, "hello", new Date()));
the callee attribute of arguments
Meaning
represents a reference to the function object itself, which is the body of the specified Function object , which is helpful for realizing the recursion of unnamed functions or ensuring the encapsulation of functions.
Usage
[function.]arguments.callee
Optional function parameter is the name of the Function object currently being executed.
Explanation
The callee attribute is a member of the arguments object and is only available when the related function is executing. The initial value of the
callee attribute is the Function object being executed. This allows anonymous recursive functions.
Example:
Use recursion to calculate the sum of natural numbers from 1 to n:
<script> var sum=function(n){ if(1==n) { return 1; } else { return n + arguments.callee(n-1); } } alert(sum(100)); </script>