It comes from thinking about a written test question. Normally, no one will modify the parameter value inside the function. It is only discussed here. There are three ways to modify it.
1. Directly modify the formal parameters when declaring the function
Copy the code code as follows:
function f1(a) {
alert(a);
a = 1;//Modify formal parameter a
alert(1 === a);
alert(1 === arguments[0]);
}
f1(10);
Function f1 defines parameter a. When calling, the parameter 10 is passed, 10 is popped out first, a is modified to 1, true is popped up twice, and a and arguments[0] are both 1.
2. Modify through the arguments object inside the function
Copy the code code as follows:
function f2(a) {
alert(a);
arguments[0] = 1; //Modify arguments
alert(1 === a);
alert(1 === arguments[0]);
}
The effect is the same as function f1.
3. The local variables declared inside the function have the same name as the formal parameters.
Copy the code code as follows:
function f3(a) {
alert(a);
var a = 1;//Declare local variable a and assign it a value of 1
alert(1 === a);
alert(arguments[0]);
}
f3(10);
Function f3 defines formal parameter a, and local variable a is declared inside the function and assigned a value of 1, but a here is still parameter a, which can be proved by the fact that the arguments[0] that pops up at the end is modified to 1.
4. If you just declare the local variable a without assigning a value, the situation is different.
Copy the code code as follows:
function f3(a) {
var a;//Declaration only, no assignment
alert(a);
alert(arguments[0]);
}
f3(10);
At this time, all the pop-ups are 10, not undefined.