JavaScript uses a variable object to track the lifetime of a variable. The basic type value is directly stored in the variable object; the reference type value is stored in the variable object as a pointer, which points to the actual object's storage location in memory.
Passing of basic type values
When passing a primitive type value to a parameter, the passed value is copied to a local variable (i.e., a named parameter, or an element in the arguments object).
function addOne (num) { num++; return num; } var count = 1; var result = addOne(count); (count); //1 (result); //2
In the above example, the value of the variable count is passed to the function parameter num for use in the function. Although the values of the variable count and the parameter num are the same, they are two independent variables. Changing the value of the parameter num in the function will not affect the value of the variable count outside the function.
Therefore, the passing of basic type value parameters of functions in JavaScript is passed by value.
Passing of reference type values
function setName (obj) { = 'Nicholas'; } var person = new Object(); setName(person); (); //'Nicholas'
In the above example, the value of the variable person is passed to the function parameter obj. At this time, a name attribute is added to the parameter obj inside the function. The function uses the parameter obj so that the variable person outside the function also obtains a name attribute. From the result, the passing of reference type value parameters of a function in JavaScript seems to be passed by reference.
However, this is not the case. The value of the variable person is a reference type value, so its value can be regarded as an address (or pointer) of an actual object in memory in the variable object. After passing the parameter, the value of parameter obj is also the address of the object in memory. Therefore, the object referenced by the value of operation parameter obj in the function is equivalent to the object referenced by the value of operation variable person.
function setName (obj) { = 'Nicholas'; obj = new Object(); = 'Greg'; return obj; } var person = new Object(); var result = setName(person); (); //'Nicholas' (); //'Greg'
If the parameter pass is passed by reference, in the above example, the function changes the object referenced by the value of the parameter obj, then the object referenced by the value of the corresponding variable person will also change. Changing the way of writing functions may be more helpful in understanding the value-by-value transfer of parameters.
function setName () { var obj = arguments[0]; = 'Nicholas'; obj = new Object(); = 'Greg'; return obj; }
Although the value of the variable person and the parameter obj are the addresses of the same object in memory, they are two independent variables. If the value of parameter obj is changed in the function to point to another object in memory, the value of the variable person will not change, or it will point to the original object.
Therefore, the passing of reference type value parameters of a function in JavaScript is passed by value.
in conclusion
All functions in JavaScript are passed by value.
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!