1, Defining parameters
1. There are two types of function parameters: formal parameter and actual parameter. A formal parameter is a variable declared by a function. It is only visible inside the function. An actual parameter is the parameter value actually passed.
//[example 1] the following code defines a simple function.
function f(a,b){ // Define function structure, transfer parameter a and b
return a+b;
}
var x=1,y=2; // Defining parameter variables
alert(f(x,y)); // Call function and pass arguments
//[example 2] for the above function, use the following method to get the number of parameters.
alert(f.length); // Return 2. Get the number of parameters of the function
//[example 3] if the number of function parameters is less than the number of parameters, the default value of the extra parameters is undefined.
(function(a,b){ // Define a function with two parameters
alert(typeof a); // Return to number
alert(typeof b); // Return to undefined
})(1); // Call function, pass an argument
//[example 4] if the number of function arguments is more than the number of formal arguments, then the extra arguments cannot be accessed through the parameter identifier, and the function will ignore the extra arguments. In the following example, arguments 3 and 4 are ignored.
(function(a,b){ // Define a function with two parameters
alert(a); // Return to 1
alert(b); // Return to 2
})(1,2,3,4); // Call function, pass four arguments
//[example 5] parameters and variables declared by var statement in function body are local variables, which are only visible in function body. When the private variable conflicts with the formal parameter, the private variable has a larger priority.
function f(a){ //Define function structure and transfer parameter a
var a = 0; //Declare private variable a with an initial value of 0
return a;
}
alert(f(5)); // Call function, pass to parameter value 5, return value 0
2. Use arguments object
The arguments object represents a collection of parameters. It is a pseudo class Array, which has a structure similar to the Array. It can access the function parameter values in the form of Array subscripts, but there is no prototype method of the basic Array.
//[example 1] in the following example, the function does not define parameters, but each parameter value passed to the function can be obtained through the arguments object in the function body.
function f(){ // Defining functions without parameters
for(var i = 0; i < arguments.length; i ++ ){
// Loop through the arguments object of the function
alert(arguments[i]); // Displays the value of the argument for the specified subscript
}
}
f(3, 3, 6); // Show each passed argument one by one
///[example 2] the arguments object can only be used in the function body as a function attribute. Users can access the arguments object through the dot operator. Because the arguments object is visible in the function body, it also directly references the arguments object.
function f(){ // Defining functions without parameters
for(var i = 0; i < f.arguments.length; i ++ ){
// Loop through the arguments object of the function
alert(arguments[i]); // Displays the value of the argument for the specified subscript
}
}
f(3, 3, 6); // Show each passed argument one by one
///[example 3] use the arguments object to edit the argument value at any time. In the following example, use the for loop to traverse the arguments object, and then pass the value of the loop variable to the argument to change the argument value dynamically.
function f(){
for(var i = 0; i < arguments.length; i ++ ){
// Traversing arguments object elements
arguments[i] =i; // Modify the value of each argument
alert(arguments[i]); // Prompt for modified argument value
}
}
f(3, 3, 6); // Return to Tips 1, 2, 3 instead of 3, 3, 6
//[example 4] by modifying the length attribute value of arguments object, you can also change the number of function parameters. When the length attribute value increases, the increased argument value is undefined. If the length attribute value decreases, the corresponding number of elements behind the arguments data set will be discarded.
function f(){
arguments.length = 2 ; // Modify the length property value of arguments object
for(var i = 0; i < arguments.length; i ++ ){
alert(arguments[i]);
}
}
f(3, 3, 6); // Return to tips 3, 3