In the traditional sense, the JavaScript function is thought to pass the reference pass (also known as the Pointer Pass), and some people think that the value transfer and the reference pass have. So JS parameter transmission in the end is how it? In fact, the following demo is also fully available for Java
First comes a relatively simple, basic type of delivery:
function Add (num) {
num+=10;
return num;
}
num=10;
Alert (Add (num));
AELRT (num);
Output 20,10
For the output 20 here, 10, according to JS official explanation is in the basic type parameter transmission, do a copy of the stack frame copy action, so that the external declaration of the variable num and function parameters of num, have exactly the same value, but have a completely different parameter address, neither know who, POPs the function argument num stack frame when the function call returns. So changing the function parameter num has no effect on the original external variables.
Let's look at a more complex delivery of object reference types:
function SetName (obj) {
obj.name= "Ted";
}
var obj=new Object ();
SetName (obj);
alert (obj.name);
Output Ted
The essence of the above code is that it creates an object, assign its reference to obj (which is directly an assignment of a memory address in C), and then, when passing the function arguments, do the same thing as the previous method, copying a stack frame to obj for the function argument, both of which have the same value ( It can be interpreted as the address of an object, and then, when SetName makes a change, it actually changes the value of the object itself (called a mutable class in Java), and after the change is complete, it also pops up the stack frame of the function parameter obj.
So the corresponding output is the value of the changed object
So perhaps some friends may ask, this can also be understood as a reference pass (pointer pass) AH? No, strictly speaking, there is no pointer in a language similar to Java, and in Java, the process is called a parsing process from a symbolic reference to a direct reference. In C, the pointer is a type with a fixed length (2 bytes in most C compilers), but in a Java-like language, 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 largely avoids memory leaks, and the term is called 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= "Marry";
}
var obj=new Object ();
SetName (obj);
alert (obj.name);
Output Ted
The 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 argument obj and the original reference obj parameter have completely different values and memory addresses.
The above is a simple discussion of the JavaScript function parameter transfer is the value of the transfer or the reference is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.