JavaScript function form participates in real parameter __ block chain

Source: Internet
Author: User

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:

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.