In traditional concepts, it is believed that JavaScript functions pass reference passing (also called pointer passing), and some people believe that both value passing and reference passing are available. So what is going on with JS parameter passing? In fact, the following demonstration can be used in Java
First, let’s get a simpler, basic type of delivery:
function add(num){ num+=10; return num;}num=10;alert(add(num));aelrt(num);//Output 20,10For the outputs 20 and 10 here, according to the official explanation of JS, a copying action is done when the basic type parameters are passed, so that the externally declared variable num and the function parameter num have exactly the same value, but have completely different parameter addresses. No one knows each other, and the function parameter num stack frame pops up when the function call returns. Therefore, changing the function parameter num has no effect on the original external variables.
Let’s look at a more complex object reference type pass:
function setName(obj){ obj.name="ted";}var obj=new Object();setName(obj);alert(obj.name);//Output tedThe essence of the above code is: create an object object, assign it reference to obj (in C, it is directly an assignment of a memory address), and then when passing the function parameters, I did the same thing as the previous method, copied a stack frame to obj of the function parameter, and both have the same value (it may be understood as the address of the object object), and then when changing the setName, the value of the object object itself (called a variable class in JAVA), and after the change is completed, the stack frame corresponding to the function parameter obj must also be popped up.
Therefore, the corresponding output is the value of the object object after changing
Then some friends may ask, this can also be understood as a reference pass (pointer pass)? No, strictly speaking, in languages similar to JAVA, there are no pointers. In JAVA, the above process is called a parsing process from symbolic reference to direct reference. In C, a pointer is a type with a fixed length (2 bytes in most C compilers), but in JAVA similar languages, references also have their own properties and methods, but you cannot directly access and control it, so it is also an object in a sense. This mechanism also greatly avoids memory leakage, and the term is called the memory structured access mechanism.
To prove the above point, the above example is slightly modified:
function setName(obj){ obj.name="ted"; obj=new Object(); obj.name="mary";}var obj=new Object();setName(obj);alert(obj.name);//Output tedThe only difference between this example and the previous example is that a new object is assigned to the function parameter obj, so that the function parameter obj and the original reference obj parameter have completely different values and memory addresses.
The above article briefly talks about whether JavaScript function parameter passing is value passing or reference passing is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.