In JavaScript Functions, the identifier arguments has a special meaning. It is a special attribute of the called object and is used to reference the arguments object. The arugments object is like an array. Note that it is just like it is not.
In JavaScript Functions, arguments is like an array (not a real array, but an arguments object, which is emphasized again). The Length attribute can represent the number of parameters passed to the function.
You can use the parameter name or the arguments [] array to reference a formal parameter. Arguments [0] indicates the first parameter.
Therefore, the arguments object in Javascript is the actual parameter of the function. Next, let's take a look at this magical country.
Arguments. Length attribute:
JS will not take the initiative to determine the number of parameters you pass to the function. If you pass more parameters, the excess parameters will not be used. If you pass less parameters, the parameter value that is not passed is undefined.
So we can use the Length attribute of arguments to check whether the actual parameters of the correct number are used when calling the function, because JavaScript will not do these tasks for you.
Function f (x, y, z)
{
// First, check whether the number of passed parameters is correct.
If (arguments. length! = 3)
{
Throw new error ("function f called with" + arguments. Length + "arguments, but it not 3 arguments .");
}
// Run the real function below
}
Arguments also provides us with the possibility of passing an arbitrary number of parameters for a function:
For example, I want to determine the size of some numbers that you pass to me, and retrieve the largest one. Yes, you can specify the number of parameters you pass, but only if you want to transmit numbers, because I am too lazy to judge inside the function. Oh.
Function max ()
{
// Based on my previous log, this is already the smallest number in JavaScript.
VaR M = number. negative_infinity;
For (VAR I = 0; I <arguments. length; I ++)
{
// If any parameter is larger than m, M becomes the value of this parameter.
If (arguments [I]> m)
M = arguments [I];
}
Return m;
}
How is it? This method is very clever, right? Haha.
It indicates that arguments is consistent with the actually passed form parameter:
For example, if you pass a parameter named Param to the function and only this parameter exists, both PARAM and arguments [0] reference this parameter value,
Change one of the values, that is, change all values of the two.
Function Change (PARAM)
{
// For example, if my Param is simaopig, alert is simaopig,
// If nothing is passed, alert undefined
Alert (PARAM );
// Use arguments [0] to change the value of this parameter.
Arguments [0] = 'xiaoxiaozi ';
// Yes. The value is changed to xiaoxiaozi.
Alert (PARAM );
}
Callee attribute of arguments:
The callee attribute of arguments is used to reference the currently executed function, which is very beneficial to the Untitled function call itself.
Do you still remember to implement recursive functions using functions directly defined in the previous article?
In this section, I mentioned that the function can be directly called. In this way, recursion can be easily called by yourself.
Now, the callee of arguments can be easily implemented.
// Use the direct amount of functions and the arguments. callee attribute to implement recursive functions
VaR result = function (x ){
If (x <= 1) return 1;
Return x * arguments. callee (x-1 );
};
At last, I would like to remind you that, since this arguments is so powerful, we should not name the variable arguments. In fact, arguments is one of the reserved words in JavaScript.
Author: simaopig
Address: http://www.xiaoxiaozi.com/2009/06/10/827/