JavaScript Advanced (V) - Function parameters are passed by value
We know that in ECMAScript
, the parameters of the function are passed by ** by value.
So how do you understand Passing by value?
Simply put, copy the value outside the function to the parameters inside the function, that is, copy the value from one variable to another variable.
That is to say, inside the function, modifying the value of the function parameter will not change the value of the external variable.
Let's take a look at an example: 2
Example 1:
var a = 1
function foo(arg) {
arg = 2
console.log(arg)
}
foo(a) // 2
console.log(a) // 1
It can be seen that the external variable a
is the parameter value of the execution time of the function foo
, and the parameter value passed in is modified inside the function to modify it. After the function is executed, no modifications to the external variable a
will occur.
This example does show that function parameters are passed by value.
But let's look at another example:
Example 2:
var obj1 = {
a: 1,
}
function foo(arg) {
arg.a = 2
console.log(arg)
}
foo(obj1) // { a: 2 }
console.log(obj1) // { a: 2 }
var obj2 = {
a: 1,
}
function bar(arg) {
arg = 2
console.log(arg)
}
bar(obj2) // 2
console.log(obj2) // { a: 1 }
In this example, after the function foo
is executed, the printed obj1
value changes, indicating that the external variable obj1
is internally modified by the function foo
. Why did the modification occur? And after the function bar
is executed, the obj2
value remains unchanged, why is this? Are the function parameters really pass by value?
So how should we understand that function parameters are passed by value
?
Before understanding this, we first need to know the data types of JavaScript
and how different data types are stored.
Data type and storage method
We know that in JavaScript
, there are two data types, namely: (1) basic data type and (2) reference data type.
- Basic data type: Values are saved directly in the ** stack (stack)**.
let a = 1
let b = a
a = 2
console.log(a, b) // 2 1
The assignment changes of the basic type in the stack are as follows:
- Reference data type: value Save in the heap and save the value in the stack The memory address of the value in the heap.
let a = { name: 'Mark' }
let b = a
b.name = 'John'
console.log(a) // { name: 'John' }
The copy changes of reference types in stack and heap are as follows:
Pass by value
We understand Pass by value
from the data type, and we can find that the value of ** passes ** refers to the value saved in the ** stack.
That is, whether the ** parameter value** is a basic data type or a reference data type, the value in the ** stack** that is passed.
For basic data types, the value of the parameter is modified internally by the function, which is actually modified by the value of the function parameter being saved in the memory fragment in the ** stack (stack)**.
For reference data types, function parameters pass the memory address of the reference type in the ** stack (stack)**:
If the value of the parameter is directly modified, the memory address saved by the function parameter in the memory fragment in the ** stack** is overwritten.
If you modify the attribute value of the parameter object, you modify the value in the heap corresponding to the memory address saved by the memory fragment in the stack** according to the function parameter.
So looking back at Example 1 and Example 2, both correctly stated that the parameters of the function are passed by value.