Home > Article > Web Front-end > A detailed introduction to passing JavaScript parameters by value
This article mainly introduces an in-depth understanding of JavaScript Parameters are passed by value. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
Definition
The parameters of all functions in ECMAScript are passed by value.
What is passing by value?
In other words, copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another variable.
Pass by value
A simple example:
var value = 1; function foo(v) { v = 2; console.log(v); //2 } foo(value); console.log(value) // 1
It’s easy to understand. When passing value to function foo, it is equivalent to copying. A copy of value, assuming that the copied copy is called _value, what is modified in the function is the value of _value, without affecting the original value.
ReferencePassing
Although copying is easy to understand, when the value is a complex data structure, copying will cause performance problems. The problem.
So there is another way of passing called passing by reference.
The so-called passing by reference means passing the reference of the object. Any changes to the parameters inside the function will affect the value of the object, because both refer to the same object.
For example:
var obj = { value: 1 }; function foo(o) { o.value = 2; console.log(o.value); //2 } foo(obj); console.log(obj.value) // 2
Hey, that’s not right. Even our Little Red Book says that the parameters of all functions in ECMAScript are passed by value. How can this be passed by reference? What about success?
And is this pass by reference?
The third delivery method
Don’t worry, let’s look at another example:
var obj = { value: 1 }; function foo(o) { o = 2; console.log(o); //2 } foo(obj); console.log(obj.value) // 1
If JavaScript uses reference pass, the outer layer The value of will also be modified. Why hasn't it been modified? So is it really not pass by reference?
This is about mentioning that there is actually a third delivery method, which is called shared delivery.
Shared transfer means that when transferring an object, a copy of the reference of the object is transferred.
Note: Passing by reference is passing a reference to the object, while passing by sharing is passing a copy of the reference of the object!
So if you modify o.value, you can find the original value through reference, but modifying o directly will not modify the original value. So the second and third examples are actually passed by sharing.
Finally, you can understand it this way:
If the parameter is a basic type, it is passed by value, if it is a reference type, it is passed by sharing.
But because the copy is also a copy of the value, it is also directly considered to be passed by value in the elevation.
So, Gao Cheng, who calls you the Little Red Book!
In-depth series
The JavaScript in-depth series is expected to be about fifteen articles, aiming to help everyone smooth out the underlying knowledge of JavaScript, focusing on explaining prototypes, scopes, and execution contexts. , variable object, this, closure, pass by value, call, apply, bind, new, inheritance and other difficult concepts.
The above is the detailed content of A detailed introduction to passing JavaScript parameters by value. For more information, please follow other related articles on the PHP Chinese website!