Maybe people don’t care too much about the parameters of a function. Simply speaking, copying values outside a function to parameters inside a function is the same as copying values from one variable to another. In-depth study, you’ll find that it’s not that simple. There are two situations in which to pass on the message (which is a mistake).,ECMAScriptThe parameters of all functions are passed by value.——《Elevation 3 The original words, here said two, because the combination of reference is easier to understand) – value reference and reference reference reference.
Value passing parameters are for basic types, reference passing parameters are for reference types, and pass parameters can be understood as duplicate variable values. The two variables are completely independent after the primitive type is copied, and then either change does not affect the other; the reference type copies the reference (pointer), and any subsequent change maps to the other..
Many people are puzzled by the fact that parameters are passed by value, because access variables can be either by value or by reference. Now let’s see what’s different:
This paragraph is very important.:We can imagine the parameters of the ECMAScript function as local variables. When you pass a basic type of value to a parameter, the value being passed is copied to a local variable (that is, a named parameter, or, in the sense of ECMAScript, one of the arguments objects)Element). When passing a reference type to a parameter, the address (pointer) of the value in memory is copied to a local variable, so that the change in the local variable is reflected outside the function.
1、Pass by value
var person = new Object(); The relationship between variables and objects can be represented in the following diagram:
When calling functionsetName(person); The following diagram can show the concern of global variable person and local variable obj:
Create an object in the code above and save it in variable person. Then, the variable is passed to the setName (obj) function and then is copied to obj. Inside this function, obj and person refer to the same object. In other words, namelyWhen ECMAScript says this variable is passed by value, obj will access the same object by reference. Then, after adding the name attribute to obj inside the function, the person outside the function will react, because the person and obj point to the sameA stack of memory addresses. So, many people mistakenly believe that objects modified in local scope are reflected in global objects, indicating that parameters are passed by reference.
To prove that the object is passed by value, let’s look at the following modified example:
obj = new Object(); //When changing the direction of obj, obj points to a new memory address, no longer pointing to the same person.4 obj.name = "Greg";
5 }
6
7 var person = new Object();
8 setName(person); //Look at the bottom. Believe me, I pass it by value.9 alert (person.name); / / "Nicholas"
obj = new Object(); Let’s take a look at the diagram of person and obj.
The only difference between this example and the previous one is that the setName () function adds two lines of code:obj = new Object(); Used to change the direction of obj;obj.name = “Greg”; Used to add attributes to newly created obj. If passed by reference, the person is automatically modified to point to the memory address of the newly created obj, and the person’s name attribute value is changed to “Greg”. However, when accessing person.name,The result is “Nicholas”. This shows that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when the obj is rewritten inside the function, this variable refers to a local object. And this local object will be executed after the function is executed.Destroy immediately!
Although the value of the variable person and the parameter obj are both addresses of the same object in memory, they are two independent variables. If you change the value of the parameter obj in a function to point to another object in memory, the value of the variable person does not change, or points to the originalThe object.
Therefore, the transfer of reference type value parameters in functions in JavaScript is passed by value.