Home >Web Front-end >JS Tutorial >Detailed explanation of parameters of javascript function_javascript skills

Detailed explanation of parameters of javascript function_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:32:571360browse

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

1.Js function 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 inputting data with parameters, 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 function

Passing by value: Just pass the value of the variable into the function. The function will allocate additional memory to save the parameter value, so the value of the original variable will not be changed.

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 Boolean---pass by value

Objects, arrays and functions---passing by address

String object————pass address

4. Parameter array of Js function

JS functions all have an 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++){
  total += sumInteger.arguments[i];
  }
  return total;
}
//调用函数
inntotal = sumInteger(100,45,567,234);
document.write("函数sumInteger(100,45,567,234):"+inttotal+"<br>");

5.Variable scope of JS function

JS function has two variables:

Local Variables are variables declared within a function. The variable can only be used within the program line within the function. Program code outside the function cannot access this variable.

Global Variables are variables declared outside the function. This variable can be accessed by the functions and program code of the entire JS program.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn