Home  >  Article  >  Web Front-end  >  Should JavaScript function parameters be passed by value (byVal) or by address (byRef)? Share_javascript skills

Should JavaScript function parameters be passed by value (byVal) or by address (byRef)? Share_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:381751browse

Regarding the question of "whether JavaScript function parameters are passed by value (byVal) or by address (byRef)", there is a common misunderstanding: "simple types" such as number and string are passed by value, Number, String, Object, Array, etc." Complex type" is pass-by-address.
Isn’t this wrong? Why is there such a misunderstanding? Take a look at these two pieces of code:

Copy the code The code is as follows:

//Cause value passing Imaginary code
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 has not changed!

Copy code The code is as follows:

//Causes the false address transmission The code of
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!

So, from the above code, we can conclude that "simple type" is passed by value (byVal) as a parameter, and "complex type" is passed by address (byRef) as a parameter.

What’s the problem?

If you carefully observe the two functions, you can find one thing:
In byVal, the parameter x is directly modified: x = 1;
And in byRef, the member of the parameter x is modified: x[0] = 4; x[1] = 5; x[2] = 6;

My guess is that in JavaScript, all variables or members are pointers. When modifying the value of a variable or member, the address of the pointer is actually modified.

So the above code can be explained:

In "byVal":

Copy code The code is as follows:

global { / / represents the global scope, and the following represents the function scope
var x = 0; // Initialize the pointer x and point to the number 0
fun(x) {
x = global.x; // Pass in Parameter global.x; The x pointer address of the fun field points to the number 0
x = 1; // Modify the x pointer address of the fun field to point to the number 1;
} // fun The domain ends, the x pointer in the global domain has not changed
}

In "byRef":
Copy code The code is as follows:

global { // represents the global scope, the following represents the function scope
/*
Initialize the pointer x and point to the array [1, 2, 3]
is actually the three members of x, 0, 1, 2, pointing to 1, 2, 3 respectively;
*/
var x = [1, 2, 3];
fun(x) {
x = global.x; // Pass in the parameter global.x; the x pointer address of the fun field points to the array [1, 2, 3]
/ + = 4;
x[1] = 5;
x[2] = 6;
} // The fun field ends, the x pointer in the global field has not changed, but its three member pointers have been changed , so we see the result we output
}


So how do you explain this code? ? ?



Copy code
The code is as follows:(function(a, b){ arguments [0] = 1;
b = 2;
console.log(arguments, a, b);
})(-1, -2);


only You can say a, b..., which is an alias for arguments[0],...[n].

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

If you have a better explanation, please share it.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn