Detail the parameters of the javascript function

  • 2020-10-23 20:00:24
  • OfStack

The javascript function can be called with any number of arguments, regardless of how many arguments are specified when the function is defined. Because a function is weakly typed, there is no way to declare the type of argument it expects, and it is legal to pass any value of any type to any function.

1. The Js function can pass in different parameters, such as


function writeNString(strMsg){
document.write(strMsg + "<br>");
}

2.Js function returns the value, js function can return the result of the run, the function can be regarded as a black box, after using the parameter input data to produce the required run result, such as


function one2N(intnumber){
var intTotal = 0;
for(var i=0;i<=intnumber;i++){
 intTotal +=i;}
return intTotal;
}

3. Pass value and address parameters of Js function

Pass value: just pass the value of the variable into the function. The function will also configure the memory to hold the parameter value, so it will not change the value of the original variable.

Address: Passes in the actual memory location of the variable, so if you change the value of the parameter in the function, you will also change the value of the original parameter.

Numbers, strings, and Booleans -- pass values

Objects, arrays, and functions -- addresses

String object -- Address

4. Array of arguments to the Js function

Js's functions each have one argument array (Arguments Array) object, called the arguments object. When a function is called to pass in parameters, the function can use the parameter array object to get the number of parameters and the individual parameter values, even if it does not specify the parameter name.


function sumInteger(){
 var total = 0;
 for(var i=0; i<sumInteger.arguments.length;i++){
  total += sumInteger.arguments[i];
  }
  return total;
}
// Call a function 
inntotal = sumInteger(100,45,567,234);
document.write(" function sumInteger(100,45,567,234):"+inttotal+"<br>");

5. Variable range of JS function

The JS function has two variables:

Local variable (local Variables) a variable declared within a function. Variables can only be used within the program line of the function, and cannot be accessed by program code outside the function.

A global variable (Global Variables) that is declared outside a function and can be accessed by the functions and program code of the entire JS program.


Related articles: