JavaScript function parameters

JavaScript function parameters

JavaScript functions allow no parameters (but the parentheses containing the parameters cannot be omitted), and parameters can also be passed to the function for use by the function.

In the following example, the name and age parameters are passed to the hello() function, and the parameter values ​​are Xiaoming and 18 respectively.

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">  
       function hello(name,age){
         document.write("我叫" + name + ",今年" + age + "岁!");
         }
    </script>  
</head>  
<body>  
   <input type="button" onclick="hello('小明',18)" value="确定" />
</body>  
</html>

JavaScript function parameter error

JavaScript function parameters do not strictly require which parameters are required parameters and which parameters are optional parameters. Therefore, the number of parameters passed in is not equal to the definition. The number of parameters in a function.

If undefined parameters are used in a function, a syntax error (undefined parameters) will be prompted and the JavaScript code will not run normally.

If the parameters have been defined but are not passed in correctly, the relevant parameter values ​​will be replaced with undefined and the JavaScript code will still run normally, as shown in the following example:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">  
       function hello(name,age){
          document.write("我叫" + name + ",今年" + age + "岁!");
         }
    </script>  
</head>  
<body>  
   <input type="button" onclick="hello('小明')" value="确定" />
</body>  
</html>

JavaScript arguments object

In JavaScript functions, there is a special arguments object, which saves the parameters of the current function call in an array-like form. Therefore, developers can easily access function parameters without defining specific parameter names:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">  
       function hello(){
        document.write("我叫" + arguments[0] + ",今年" + arguments[1] + "岁!");
      }
    </script>  
</head>  
<body>  
  <input type="button" onclick="hello('小明',18)" value="确定" /> 
</body>   
</html>

In the arguments object, arguments[0] represents the first parameter, and arguments[1] represents the second parameter. ,And so on.

Tips:

Usually in function definitions, in order to facilitate the readability of the code, the arguments object is generally not used. When dealing with an indefinite number of parameters or simulating function overloading, the arguments object can be conveniently used.

Detecting the number of parameters

Using the arguments object, you can easily detect the number of parameters of a function. The length attribute of arguments, that is, arguments.length, is the number of parameters:

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
    <script type="text/javascript">  
       function hello(){
        document.write(arguments.length);
      }
    </script>  
</head>  
<body>  
  <input type="button" onclick="hello('小明',18)" value="确定" />  
</html>


1. Js functions can pass in different parameters, such as

function writeNString(strMsg){

##document.write(strMsg + "<br>");

}

2. Js function returns a value. The js function can return the running result. The function can be regarded as a black box. After using parameters to input data, the required running result is generated, such as

function one2N(intnumber){

var intTotal = 0;

for(var i=0;i< =intnumber;i++){

intTotal +=i;}

return intTotal;

}

3. Value-passing and address-passing parameters of Js functions

Passing by value: Just pass the value of the variable into the function, and the function will configure additional memory to save the parameter value, so It does not change the value of the original variable.

Pass address: Pass the memory location where the variable is actually saved into the function, so if the value of the parameter is changed in the function, the value of the original parameter will also be changed at the same time.

Numbers, strings and booleans----passing by value

Objects, arrays and functions----passing by address

String objects----- -Passing address

4. Parameter array of Js function

Js functions all have a parameter array (Arguments Array) object, called arguments object. When calling a function to pass in parameters, even if the function does not specify the parameter name, it can still use the parameter array object to obtain the number of parameters and individual parameter values.

function sumInteger(){

var total = 0;

for(var i=0; i<sumInteger.arguments.length;i++){

##                                                                         1 ## Return total;

}

//Call function

inntotal = sumInteger(100,45,567,234);

##document.write("Function sumInteger(100,45,567,234):"+inttotal+"<br>");

5. Variable scope of JS functionsJS functions have two types of variables:

Local variables (local Variables) are variables declared within the function. Variables can only be declared within the function. This variable is used within the program line, and program code outside the function cannot access this variable. Global variables (Global Variables) are variables declared outside the function. This variable can be accessed by the functions and program code of the entire JS program.

Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <script type="text/javascript"> function hello(name,age){ document.write("我叫" + name + ",今年" + age + "岁!"); } </script> </head> <body> <input type="button" onclick="hello('小明')" value="确定" /> </body> </html>
submitReset Code