Home > Web Front-end > JS Tutorial > A brief discussion on the modifiability of JavaScript function parameters_javascript skills

A brief discussion on the modifiability of JavaScript function parameters_javascript skills

WBOY
Release: 2016-05-16 17:10:43
Original
1494 people have browsed it

I came 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 The code is as follows:

function f1(a) {
alert(a);
a = 1;//Modify the formal parameter a
alert(1 === a);
alert (1 === arguments[0]);
}
f1(10);

Function f1 defines parameter a. When calling, pass parameter 10, pop up 10 first, modify a is 1, true pops up twice, a and arguments[0] are both 1.

2, modify


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 The code is as follows:

function f3(a) {
alert(a);
var a = 1;//Declare local variable a and assign a value of 1
alert(1 === a);
alert(arguments[0]);
}
f3(10);

Function f3 defines the formal parameter a, and declares the local variable a inside the function. The assignment is 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 local variable a without assigning a value, the situation is different

Copy code The code is as follows:

function f3(a) {
var a;//Only declaration, no assignment
alert(a);
alert(arguments[0 ]);
}
f3(10);

What pops up at this time is 10, not undefined.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template