Modifiable JavaScript function parameters _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces the modifyability of JavaScript function parameters in detail. If you need a friend, you can refer to it and hope to help you think about it, generally, no one modifies the parameter value within the function. We will only discuss it here. There are three ways to modify it.

1. directly modify the form parameter of the function declaration.

The Code is as follows:


Function f1 (){
Alert ();
A = 1; // modify the parameter
Alert (1 = );
Alert (1 === arguments [0]);
}
F1 (10 );


The f1 function defines parameter a. If you call this function, the value 10 is first displayed, and the value a is changed to 1. The value true is displayed twice. Both a and arguments [0] are set to 1.

2. Use the arguments object in the function to modify

The Code is as follows:


Function f2 (){
Alert ();
Arguments [0] = 1; // modify arguments
Alert (1 = );
Alert (1 === arguments [0]);

}


The effect is the same as that of function f1.

3. The local variables declared in the function have the same name as the form parameter.

The Code is as follows:


Function f3 (){
Alert ();
Var a = 1; // declare the local variable a and assign the value to 1.
Alert (1 = );
Alert (arguments [0]);
}
F3 (10 );


Function f3 defines the parameter a. The function declares that the local variable a is assigned a value of 1 at the same time, but here a is still the parameter, the last pop-up arguments [0] is changed to 1 to prove.

4. If we only declare the local variable a but do not assign values, the situation will be different.

The Code is as follows:


Function f3 (){
Var a; // declare only, no value assigned
Alert ();
Alert (arguments [0]);
}
F3 (10 );


In this case, 10 is displayed instead of undefined.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.