This article is based on the fourth chapter of the Red Book JavaScript Advanced Program design, the 4.1.3 Pass parameter subsection P70, to further understand the parameters of the function in Javasript, when the passed parameter is the object's delivery mode.
(combined with the personal understanding of the information, there is an incorrect place, I hope you point out, thank you!) )
The reference information is as follows:
https://github.com/simongong/js-stackoverflow-highest-votes/blob/master/questions21-30/ Parameter-passed-by-value-or-reference.md
1190000005177386
First, let's take a quick look at what is passed by value and passed by reference.
Pass by value : Copies the values from the outside of the function to the parameters inside the function, rather copying the values from one variable to the other, which is not affected by the two variables. (Transfer memory copy)
eg
1 function Addten (num) {2 num+=10; 3 return num; 4 }5 var count=20; 6 var result=Addten (count); 7 alert (count); // - 8 alert (result); // -
Pass by reference : passes a memory pointer.
By reference, when a value is passed to a parameter, the address in memory is passed, so the operation on the value in the local scope is reflected in the global scope. Because they operate on the same address.
However, in ECMAScript, when you pass a value of a reference type to a parameter, if you say "pass by value" directly, the result of the following code is confusing:
Code1:
1 function setName (obj) {2 obj.name= "linshuling"; 3 }4 var person=New Object (); 5 setName (person); 6 alert (person.name);//linshuling
But if this is the case because it is not "passed by value" but "passed by reference", we cannot explain the following:
Code2:
1 functionsetName (obj) {2Obj.name= "Linshuling";3 obj=New Object (); 4 obj.name= "Lin"; 5 }6 varperson=NewObject ();7 setName (person);8 alert (person.name); // linshuling
So, I found another explanation on the Internet:
Call-by-sharing a copy of the reference.
When I re-read this explanation in the book, I found that this is consistent with the explanation in the book: when you pass a value of a reference type to a parameter, the value is copied to a local variable in an in-memory address, so the change of the local variable is shown outside the function. (note here that the address is copied to a local variable instead of directly to the address.) )
That's what I understand:
Combine the above understanding Code1,code2:
Obj.name= "Linshuling" in Code1, which is reflected in theglobal scope because it actually operates on the same address, such as the address of a after copy of Add, because add and ad are the same value, it also affects a. But in Code2 obj=new Object (); obj.name= "Lin"; this is actually rewriting obj inside the function, which overrides the add, but it does not affect the ad, and the value of add and ad is completely independent (from here to understand that the function's parameter pass is passed by value, and it's good to understand!) ), and the redefined obj here is a local variable that will be destroyed immediately after the function has been executed.
How to understand that the parameters of a function in Javasript are passed by value