First, to see what the hell is it?
The arguments object should be counted as a special object in JS. The creation that cannot be displayed is only available at the time of the current function call and looks a bit like an array, but is definitely not an instance of array.
A few notes:
1.1, arguments is actually a built-in property of the current function, within the current function, the name of the functions. Arguments is equivalent to arguments, and it is meaningless to use arguments alone outside the function;
1.2,the length of the arguments object is determined by the number of actual parameters.
1.3, the formal parameters and arguments in the function can affect each other.
It can be seen that when the number of arguments passed is less than the number of parameters, the actual parameter is the subject.
In fact, the arguments to the function are already stored in the arguments object.
--------------------------------------------------------------------------------------------------------------- -
Second, how can we use this arguments object?
2.1, check the number of parameters passed is correct?
function Func1 (a,b,c) { if (arguments.length!=3) { trow new Error (' Incorrect number of arguments ') } /*****dosomthing***** **/} and so on, we can also restrict the types of incoming parameters.
2.2 Emulation implements overloading.
JS can not be like Java through different parameter types or number to implement the function of overloading, why??? Because the function is actually an object, the function name is the identifier of the function, which is simply a variable, and the parameter is just the property of the function. It is not possible to implement overloading of functions by defining the number of parameters.
When the function is called, JS is the function name to find the corresponding function, and then according to the function definition time parameters, and the expression parameter list in order to match, the redundant parameters discarded, not enough with undefined complement, then since the function in the call back to produce a arguments object, Are we going to be able to take advantage of the overload of the simulation function???
Simulates function Func2 (arg1, AGR2, ARG3) according to the parameters { var sum; if (arguments.length = = 1) { sum = arg1+100; } else{ sum =arg1+arg2+arg3; } return sum;} Simulate function Func2 (ARG) { if (typeof arg1 = = ' number ') { /** operation 1**/ }else if (typeof arg1 = =) According to the parameter's value//parameter type String ') { /** operation 2**/ }}
-------------------------------------------------------------------------
Third, the last arguments also has a callee property, from the results printed above, CALLEE returns is the function object being executed, that is, the body of the object.
So, callee still has the length, actually arguments.callee.length== the length of the parameter. When you say so much, when do you usually use it???
3.1 You can use the length of the arguments.callee.length== parameter to verify that the number of parameter arguments is consistent.
3.2 Using callee is a reference to the function itself, which handles the recursive problem of anonymous functions.
Seek 1~100 's and, just an example. 1, if we use the general recursive function var sum = function (n) { if (1==n) { return 1; } else{ return N+sum (n-1); }} Now use Calleevar sum = function (n) { if (n <= 0) return 1; else return n + arguments.callee (n-1)}
/
What's the difference?
The former contains a call to sum itself, and the Var sum is equivalent to a global variable, and each invocation is equivalent to calling a global variable. The latter did not, but took advantage of the callee attribute, and the word is not recursive (careful as), _^^_. To tell the truth, it should be very few people to write so.
On arguments object in JS