In the JavaScript function body, the identifier arguments has a special meaning. It is a special property of the calling object that is used to refer to the arguments object. Arugments objects are like arrays, note that this is just like not ha. In the JavaScript function body, arguments is like an array (not a real array, a arguments object, again), with the length property, which can represent the number of arguments passed to the function. The reference to a formal parameter can be either a parameter name or a arguments[] array, where Arguments[0] represents the first argument. Arguments.length attribute: JS will not take the initiative for you to determine how many parameters you give the function, if you pass more, the extra part is not used, if you pass less, then the parameter value is undefined so we can use the length property of arguments to detect the calling function When the correct number of actual parameters are used, because JavaScript is not doing these things for you function f (x, Y, z) {//first check whether the number of parameters passed is correct if (arguments.length!=3) {thrownew ERR or ("function f called with" + arguments.length+ "arguments, but it is not 3 arguments."); }//Run the real function below}arguments also provides us with the possibility of passing an arbitrary number of actual parameters for a function: for example, I want to determine the size of some of the numbers you pass to me, take the biggest one, right, yes, you can pass the number of arguments, but only if you want to pass the numbers, Because I'm too lazy to judge inside the function. Oh. function Max () {///According to my previous log, this is already the smallest number in JavaScript, var m = number.negative_infinity; for (var i =0; i < arguments.length; i++) {//As long as any one parameter is larger than M, then m becomes the value of this parameter if (arguments[i]> m) m = Arguments[i]; } return m;} What do you think? This is a clever method, isn't it? Oh. Explain that the arguments is consistent with the formal parameters of the true pass: for example, you pass a parameter called Param to the function, and only thisparameters, then param and arguments[0] are references to this parameter value, changing one of the values, that is, changing the value of both function change (param) {//For example, I pass param to Simaopig, Then alert is simaopig,//If nothing is passed, alert undefined alert (param); Using arguments[0] Changed the value of this parameter arguments[0]= ' Xiaoxiaozi '; Yes, this value becomes Xiaoxiaozi alert (param);} The callee property of arguments: Arguments's Callee property is used to refer to the currently executing function, which is very useful for unnamed function calls themselves. Now with the arguments of this callee can also be easily implemented//with the function of the direct amount, using the Arguments.callee property to implement the recursive function var result =function (x) {if (x<=1) return1; Return X*arguments.callee (x-1);}; At the end of the note, since this arguments is so powerful, we should not name the variable arguments, in fact arguments is one of the reserved words of JavaScript.
The role of arguments in JS