JavaScript function arguments are values (byVal) or pass-by (BYREF) sharing

Source: Internet
Author: User
Tags join

For the question of whether the JavaScript function parameter is a byVal or a pass (BYREF), there is a common misconception that "simple types" such as number,string are values, number, String, Object, array, and so on "complex types." is the address.
Isn't that right? Why is there such a misunderstanding? Take a look at these two pieces of code:

Copy Code code as follows:


//The code that creates the illusion of passing value


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


//
code to create a false address

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 has changed!


As a result, the above code concludes that "simple Type" as a parameter is a value (ByVal), and that "complex type" is a pass (byRef) argument.

What's the problem?

With a careful look at two functions, you can find a point:
In ByVal, The parameter x:x = 1 is directly modified;
In ByRef, the member that modifies the parameter x: x[0] = 4; X[1] = 5; X[2] = 6;

From this I guess: in JavaScript, all variables or members, are a pointer, when modifying a variable or member value, it is actually modified the address of the pointer.

So the code above can be explained:

In "ByVal":

Copy Code code as follows:


Global {//Represents a global scope, the following representation function scope


var x = 0; Initializes the pointer x and points to the number 0


Fun (x) {


x = global.x; Incoming parameter global.x; The X-pointer address of the fun field points to the number 0 as the X pointer address of the global field


x = 1; Modifies the X-pointer address of the fun field, pointing to the number 1;


The//Fun field ends and the x pointer in the global field does not change


}


In "ByRef":

Copy Code code as follows:


Global {//Represents a global scope, the following representation function scope


  /*


initializes the pointer x and points to the array [1, 2, 3]


is actually three members of X 0, 1, 2, respectively pointing to 1, 2, 3;


  */


var x = [1, 2, 3];


Fun (x) {


x = global.x; Incoming parameter global.x; The X-pointer address of the fun field points to the array as the X-pointer address of the global field [1, 2, 3]


      /*

The x
in the Fun field is not changed again


then modifies the pointer to the X (i.e. global.x) three member pointers in the fun domain


      */


x[0] = 4;


x[1] = 5;


X[2] = 6;


}//Fun domain ended, the x pointer in the global field did not change, but its three member pointers were changed, so we saw the results of our output


}


So how does this code explain???

Copy Code code as follows:


(function (A, b) {


arguments[0] = 1;


B = 2;


Console.log (arguments, a, b);


}) (-1,-2);


Can only say A, b ..., is arguments[0],... [N] alias.

If there is something wrong, please point it out, thank you.

If there is a better explanation, you are welcome to share.

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.