In addition to passing by value, passing by reference, there is a third method of passing by share
define
In JavaScript Advanced Programming, page 3, 4.1.3, we talk about passing parameters:
Arguments to all functions in ECMAScript are passed by value.
What is passing by value?
That is, copying a value from outside a function to an argument inside a function is the same as copying a value from one variable to another.
Pass by value
Here’s a simple example:
var value = 1;
function foo(v) {
v = 2;
console.log(v); / / 2
}
foo(value);
console.log(value) / / 1Copy the code
When you pass a value to foo, you copy a value. Let’s say the copy is called _value. The function changes the value of _value without affecting the original value.
reference
Copying is well understood, but when values are complex data structures, copying can cause performance problems.
So there’s another way of passing called passing by reference.
Passing by reference refers to passing a reference to an object, and any changes to parameters within a function affect the value of that object because they refer to the same object.
Here’s an example:
var obj = {
value: 1
};
function foo(o) {
o.value = 2;
console.log(o.value); / / 2
}
foo(obj);
console.log(obj.value) / / 2Copy the code
No, even our Little Red Book says that all ECMAScript functions are passed by value. How can this be passed by reference?
Is this passing by reference?
The third method of transmission
No hurry, 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) / / 1Copy the code
If JavaScript is passed by reference, the outer values will be changed too. So it’s really not pass-by-reference?
And that brings us to the third method of delivery, which is called shared delivery.
Shared passing is when you pass an object, you pass a copy of the reference to the object.
Note: passing by reference is passing a reference to an object, while passing by share is passing a copy of a reference to an object!
So if you change O. value, you can find the original value by reference, but if you change O directly, you won’t change the original value. So the second and third examples are actually shared.
Finally, you can understand it this way:
Parameters are passed by value if they are of basic type and shared if they are of reference type.
But because a copy copy is also a copy of a value, it is also considered passed by value in elevation.
So, high, who call you are red book!
Next article
JavaScript deep simulation of call and apply
In-depth series
JavaScript In-depth series directory address: github.com/mqyqingfeng… .
This in-depth JavaScript series is designed to help you understand the basics of JavaScript, including prototypes, scopes, execution contexts, variable objects, this, closures, passing by value, call, apply, bind, new, and inheritance.
If there is any mistake or not precise place, please be sure to give correction, thank you very much. If you like or are inspired by it, welcome star and encourage the author.