JavaScript function parameters are value-passing (byVal) or pass-through (BYREF) sharing _javascript Tips

Source: Internet
Author: User

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:

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

Code that creates a false message
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 globals scope, the following representation function scope
var x = 0; Initialize pointer x and point to 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, the x pointer in the global field does not change
}

In "ByRef":
Copy Code code as follows:

Global {//Represents a globals scope, the following representation function scope
/*
Initializes the pointer x and points to the array [1, 2, 3]
is actually X's three members 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]
/*
X in the Fun field is not changed again
Then modify the pointer of the x (i.e. global.x) three member pointers in the fun field
*/
X[0] = 4;
X[1] = 5;
X[2] = 6;
The//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.