Share whether the JavaScript function parameter is "byVal" or "byRef"

Source: Internet
Author: User

For the question of "whether JavaScript function parameters are value passing (byVal) or address passing (byRef)", there is a common misunderstanding that "simple type" such as number and string is value passing, Number, string, Object, Array, and other "complex types" are data transfer addresses.
Isn't that true? Why is there such a misunderstanding? Let's take a look at the two codes:
Copy codeThe Code is as follows:
// Code that creates the illusion of passing values
Function modifyLikeByVal (x ){
X = 1;
Console. log ('X = % d', x );
}
Var x = 0;
Console. log ('X = % d', x); // output x = 0
ModifyLikeByVal (x); // output x = 1
Console. log ('X = % d', x); // output x = 0 x not changed!

Copy codeThe Code is as follows:
// Code that creates the illusion of an address transfer
Function modifyLikeByRef (x ){
X [0] = 4;
X [1] = 5;
X [2] = 6;
Console. log ('X = [% s] ', x. join (','));
}
Var x = [1, 2, 3];
Console. log ('X = [% s] ', x. join (', '); // output x = [1, 2, 3]
ModifyLikeByRef (x); // output x = [4, 5, 6]
Console. log ('X = [% s] ', x. join (', '); // output x = [4, 5, 6] x changed!

As a result, the code above concludes that "simple type" is passed as a parameter (byVal), and "complex type" is passed as a parameter (byRef.

What is the problem?

Observe the two functions carefully to find one point:
In byVal, the parameter x: x = 1 is directly modified;
In byRef, x [0] = 4; x [1] = 5; x [2] = 6;

I guess from this: In JavaScript, all variables or members are a pointer. When modifying a variable or member value, the address of the pointer is actually modified.

The above code can be explained as follows:

In "byVal:
Copy codeThe Code is as follows:
Global {// indicates the global scope, and the following indicates the function Scope
Var x = 0; // initialize pointer x and point to number 0
Fun (x ){
X = global. x; // input the global. x parameter. The x pointer address of the fun domain points to the number 0 in the same way as the x pointer address of the global domain.
X = 1; // modify the x pointer address of the fun domain, pointing to the number 1;
} // The fun domain ends. The x pointer in the global domain has not changed.
}

In "byRef:
Copy codeThe Code is as follows:
Global {// indicates the global scope, and the following indicates the function Scope
/*
Initialize pointer x and point to array [1, 2, 3]
In fact, the three members of x are 0, 1, and 2, respectively pointing to 1, 2, and 3;
*/
Var x = [1, 2, 3];
Fun (x ){
X = global. x; // input the global. x parameter. The x pointer address of the fun domain is the same as the x pointer address of the global domain and points to the array [1, 2, 3].
/*
X in the fun domain has not been changed
Then modify the pointer pointing to the three member pointers of x (global. x) in the fun domain.
*/
X [0] = 4;
X [1] = 5;
X [2] = 6;
} // When the fun domain ends, the x pointer in the global domain is not changed, but the three member pointers are changed, so we can see the output result.
}

How can I explain this code ???
Copy codeThe Code is as follows:
(Function (a, B ){
Arguments [0] = 1;
B = 2;
Console. log (arguments, a, B );
}) (-1,-2 );

It can only be said that a, B... is the alias of arguments [0],... [n.

If something is wrong, please point it out. Thank you.

If you have a better explanation, please share it with us.

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.