About whether JavaScript function parameters are passed through values or references, and about javascript
In the traditional concept, JavaScript Functions are considered to be passed by reference (also called pointer transfer), and some people think that both value transfer and reference transfer are available. So what is the transfer of JS parameters? In fact, the following demo can be used in Java
First, a simple transfer of basic types:
Function add (num) {num + = 10; return num;} num = 10; alert (add (num); aelrt (num); // output 20, 10
For the output of 20 and 10, according to the official explanation of JS, a copy action is performed to copy the stack frame when the basic type parameter is passed, in this way, the variable num in the external Declaration and the num in the function parameter have identical values, but have completely different parameter addresses. No one knows either of them, the num stack frame of the function parameter is displayed when the function call returns. Therefore, changing the function parameter num has no effect on the original external variables.
Let's look at a complicated transfer 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 code above is: Create an object and assign its reference to obj (in C, it is directly a memory address value assignment ), then, when passing the function parameters, we did the same thing as the previous method and copied a stack frame to the obj of the function parameter, the two have the same value (it may be understood as the address of the object), and when the setName is changed, in fact, it changes the value of the object itself (called a variable class in JAVA). After the change is completed, the stack frame corresponding to the function parameter obj should also pop up.
Therefore, the corresponding output changes the object value.
Some may ask, can this be understood as a reference transfer (pointer transfer? No, strictly speaking, in a language similar to JAVA, there is no pointer. in JAVA, the above process is called a parsing process from symbolic reference to direct reference. In C, the pointer is a type with a fixed length (two bytes in most C compilers), but in a similar JAVA language, references also have their own attributes and methods, but you cannot directly access and control it, so it is also an object in a sense, this mechanism avoids Memory leakage to a large extent. It is called a memory structured access mechanism.
To prove the above point of view, we will slightly rebuild the above example:
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 parameter obj and the original reference obj parameter, has a completely different value and memory address.
The above discussion about whether the passing of JavaScript function parameters is a value transfer or a reference transfer is all the content shared by Alibaba Cloud. I hope you can provide a reference and support for our customer base.