Is the JavaScript function parameter a value (byval) or an address (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? Take a look at the two paragraphsCode:

 
//Code that creates the illusion of passing valuesFunctionModifylikebyval (x) {x= 1; Console. Log ('X = % d', X );}VaRX = 0; Console. Log ('X = % d', X );//Output x = 0Modifylikebyval (X );//Output x = 1Console. Log ('X = % d', X );//Output x = 0 x not changed!

 

//Code that creates the illusion of data transferFunctionModifylikebyref (x) {x [0] = 4; X [1] = 5; X [2] = 6; Console. Log ('X = [% s] ', X. Join (','));}VaRX = [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:

Global {//Indicates the global scope, and the following indicates the function scope.VaRX = 0;//Initialize pointer X and point to number 0Fun (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 number 0.X = 1;//Modify the X pointer address of the fun domain to point to number 1;}//The fun domain ends, and the X pointer in the global domain remains unchanged.}

In "byref:

Global { //  Indicates the global scope, and the following indicates the function scope.    /* The initialization pointer X points to the array [1, 2, 3], which is actually the three Members of x 0, 1, 2, respectively pointing to 1, 2, 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 to the array [1, 2, 3].        /*  The X in the fun domain is not changed, and then the pointer of the three member pointers in the fun domain X (that is, global. X) is modified.  */  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 ???

 
(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.

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.