Arguments Object
In JavaScript, when it comes to function parameters, you have to talk about the familiar and unfamiliar argunemts.
- The arguments object is used to host the arguments passed in when the function is called
- Arguments is not an array, but it is very similar to an array, with the length property, or you can use the subscript to access
-
The length of the arguments object is determined by the number of arguments, not the number of parameters. A parameter is a variable inside a function that re-opens the memory space, but it does not overlap with the arguments object memory space. In the case of arguments and values, the values are synchronous, but for one of the none, the value of this no value is not synchronized. The following code can be verified.
functionF (A, B, c) {console.log (arguments.length);//Result: "2"A = 100; Console.log (arguments[0]);//Result: "Arguments[0] = ' Qqyumidi '; Console.log (a); //Result: "Qqyumidi"Console.log (c);//result: "Undefined"c = 2012; Console.log (arguments[2]);//result: "Undefined"}f (1, 2);//actually, only two parameters were passed.
It is shown here that the value of the internal parameter is not synchronized with the value of the arguments position when the formal parameter inside the function is initially empty.
(This reference is from http://www.cnblogs.com/lwbqqyumidi/archive/2012/12/03/2799833.html)
- in JavaScript, the function name is the same, the number/type of arguments is different, and is not considered to be two functions . Does not overload the function like Java, the arguments object can be used to simulate overloaded effects in JavaScript .。
function Fun () { if (arguments.length = = 0 // Span style= "color: #008000;" > do sth; if (arguments.length = 1 if (typeof (arguments[0]) = = ' function ' ' { do sth for function if (Argum Ents.length = = 2 do sth;
- The number of parameters passed in JavaScript does not affect the function being called
function Fun () { return arguments[0] + arguments[1];} Fun (3,5); // 8Fun (3); // NaNFun (); // NaN
Example Aruguments[0] and aruguments[1], if not passed in, the value is Nan
- There is a practical property in the arguments object: callee
function Count (a) { console.log (Arguments.callee);} Count (10); // Count (a)
Returns the function itself
Incoming excess parameters
- What happens if I pass too many arguments when I call a function?
- Use the "... rest" method when declaring a function to represent the remaining parameters (rest is an arbitrary legal name, an array)
function Fun (A, B, ... restarray) { Console.log (' We have received: ' + A + ' with ' + B ' ); var count=2; for (var x of Restarray) { count+ +; Console.log ("+count+" is: "+x); }} Fun (1, 8, 9, 5,8,9,6,4,8,7,8,9); /* we received: 1 and 8 The 3rd is: 9 The 4th is: 5 5th is: 8 6th are: 9 7th are: 6 8th are: 4 9th are: 8 10th are: 7 11th are: 8 12th yes: 9 */
JavaScript will use an array of custom names to store the extra parameters.
Insufficient parameters passed in
When calling a function, the incoming parameters are not enough (the incoming parameters are less than the received), and JavaScript will replace the missing parameters with undefined. In this way, it is possible to affect the internal operation of the original function, now ES 6 is implemented to allow the default value of the parameter, when the parameters are missing, directly use the default value.
function Fun (a = "I am a", B = "I am B", c) { + "" + B + "" + C); } Fun ("I am a cousin of a"); // I'm a cousin of a, and I'm b undefined.
JavaScript Advanced (7)---function parameters