A parameter, which is used when defining a function, is used to receive actual parameters that call the function newsletters in.
Arguments: Arguments that are passed to a function at call time
For example
function Myfun (a,b,c) {
...
}
Myfun (1,2,3);
Here a,b,c is the formal parameter. 1,2,3 is the actual parameter.
Formal parameters and arguments are different variables, they are in different places in memory, and the parameters are freed at the end of the function run.
There are two main points to be aware of
1. If the argument is a reference type (Object,array, etc.), changes to the relative parameters affect the value of the argument
var obj = {
name: ' obj ',
age:12
}
function MyFunc (objtemp) {
objtemp.name= ' func ';
alert (objtemp.age);
myfunc (obj);
alert (obj.name); Func
So the last sentence: alert (obj.name); What will output, the answer is func.
Function parameter passing consists of two ways: value passing and reference passing.
Value passing: A formal parameter is a copy of the argument value, and changes to the formal parameter do not affect the argument
Reference passing: The formal parameter is actually a copy of the argument reference variable, which causes the argument and the formal parameter to point to the same object entity. Formal parameter changes change the value of the argument at the same time.
This can be understood as follows: MyFunc (obj);
Objtemp = obj;
Objtemp.name = ' func ';
alert (objtemp.age);
An example is provided:
var arr= [' Obj1 ', ' obj2 ', ' obj3 ']
function MyFunc (arrtemp) {
arrtemp[1] = ' myfunc ';
alert (arrtemp);
}
MyFunc (arr); Obj1,myfunc,obj3
alert (arr[1]);//myfunc
2. The form participates in the processing of the local variable's duplicate name.
function MyFunc (a) {
alert (a); Hello
var a = + ' world ';
var b = A;
alert (a); HelloWorld
alert (b); HelloWorld
}
myfunc ("Hello");
Because of "variable declaration elevation", formal parameter a already exists when the first alert (a) is executed. Local variable A is declared but not assigned, and local variable A is not created in memory. At this point alert (a), a represents the formal parameter, so the output "hello".
To execute var a = + ' world ', when the right A is a formal parameter, the left is a local variable. These two aces do not interfere with each other. But after the execution of this sentence, the local variable A has overridden the formal parameter a. So at the back of the B=a, A is a local variable.
In general, when a local variable is not declared or declared but not assigned a value, there is no such variable in memory, and the variable with the same name as the call is a formal parameter. When the local variable assignment is complete, the variable already exists in memory and the formal parameter with the same name is overwritten. When you call the variable later, you refer to the local variable.
Example: