In the third edition of JavaScript Advanced Programming, 4.1.3, about passing parameters:
Parameters for all functions in ECMAScript are passed by value
Pass by value
That is, to copy the values from the outside of the function to the parameters inside the function, and to copy the values from one variable to another.
var value = 1;function foo(v) { v = 2; console.log(v); //2}foo(value);console.log(value) // 1
When passing value to the function foo, it is equivalent to copying a copy of value to Foo if the copy of the share is called V, the function is modified by V, not an original value
Reference delivery
Passing copies of the contents by value it's good to understand. But when the values are a complex data structure, the copy produces performance problems
So there's another way to pass it, called by reference .
The so-called pass-by-reference is a reference to the object, and any changes to the parameters within the function will affect the value of the object, because both refer to the same object
var obj = { value : 1 }; let foo = (o)=> { o.value = 2; console.log(o.value); } foo(obj) console.log(obj.value);
A question arises here?
Red Book said that the parameters of all functions in the ECMAScript are passed by value, how can this be done by "reference passing"?
Let's look at a third 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 passing, the outer values are also modified, so why hasn't it been changed? So is it really not a reference pass?
This is to say that there is actually a third way of delivery, called by share delivery.
A shared pass is a copy of a reference to the object that is passed when the object is passed.
Key points:
=
an operator is the creation or modification of a variable's point in memory.
The variable is created when it is initialized, and is modified as a value for re-assignment.
In order to explain the above shared pass here is looking at an example to find out the distribution in memory
var a = {b: 1};// a = {b: 1}var c = a;// c = {b: 1}a = 2;// 重新赋值aconsole.log(c);// {b: 1}
- Create variable A to point to object {b:1}
- Create variable C to point to object {b:1}
- A again points to the constant 2
But this time C still points to object {b:1}
So we look back at the first example
var value = 1;function foo() { var v = value; // 创建变量v指向value所指向的值 v = 2;// v重新指向另外的值 console.log(v); //2}foo(value);console.log(value) // 1,value从始至终都未改变指向.
Now, the first example changes to an object
var a = {b: 1};// a = {b: 1}var c = a;// c = {b: 1}a.b = 2;// 重新赋值对象a中的属性bconsole.log(c);// {b: 2}
Stack |
Heap |
Constant Area |
A,c |
[Object] |
|
B |
|
1 |
After execution a.b = 2
:
Stack |
Heap |
Constant Area |
A,c |
[]object |
|
B |
|
2 |
A,c no change from beginning to end, it's a B.
Now let's look at the second example.
var obj = { value: 1};function foo() { var o = obj; o.value = 2;// 变量value改变了指向,而o并未改变 console.log(o.value); //2}foo(obj);console.log(obj.value) // 2
So JS is always passed by value, where he is called a shared pass
JavaScript in-depth parameters are passed by value