Tips: This article is after listening to the four-foot cat JS course after the search, in-depth understanding can see two blog:
JavaScript data types-value types and reference types
JavaScript data manipulation-the nature of primitive and reference values
In the traditional sense, JavaScript functions are considered to pass reference passing (also known as pointer passing), and some people think that both value passing and reference passing are available. So what is the parameter transfer of JS? In fact, the following demos are also fully available for Java
First, 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 here 20, 10, according to the official JS interpretation is when the basic type parameter is passed, do a copy of the stack frame copy action, so that the external declaration of the variable num and the function parameter num, have exactly the same value, but have a completely different parameter address, the two who do not know who, 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 variable.
Let's look at a more complex, object-reference-type delivery:
function setName (obj) { Obj.name= "Ted";} var obj=New Object (); setName (obj); alert (obj.name) ; // output Ted
The essence of the above code is to create an object, assign its reference to obj (the assignment of a memory address in C), and then, when passing a function parameter, do the same thing as the previous method, copying the stack frame to the function parameter, obj, with the same value ( Think of it as the address of the object, and then change the value of the object itself (referred to as a mutable class in Java) when the SetName is changed, and also eject the stack frame of the function parameter obj after the change is complete.
So the corresponding output is the value of the changed object object.
Then perhaps some friends may ask, so it can also be understood as a reference pass (pointer passing) AH? No, strictly speaking, in a language similar to Java, there is no pointer, and in Java this process is referred to as 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, the reference has its own properties and methods, but you cannot directly access and control it, so it is a kind of object in a sense, This mechanism also largely avoids memory leaks, and the term is called memory structured access mechanism.
To justify the above, slightly retrofit 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, have completely different values and memory addresses.
Whether the JavaScript function parameter pass is a value pass or a reference pass