This article mainly introduces the passing method of JavaScript function parameters, which has good reference value. Next, let's take a look at it. This article mainly introduces the transmission method of JavaScript function parameters, which has good reference value. Let's take a look at it with the small Editor.
JavaScript uses a variable object to track the lifetime of a variable. The basic type value is directly stored in the variable object, and the reference type value is saved as a pointer in the variable object, pointing to the actual storage location of the object in the memory.
Transfer of basic type values
When you pass a basic type value to a parameter, the passed value is copied to a local variable (named parameter, or an element in the arguments object ).
function addOne (num) { num++; return num;}var count = 1;var result = addOne(count);console.log(count); //1console.log(result); //2
In the preceding example, the value of the variable count is passed to the parameter num of the function for use in the function. In this case, the value of the variable count and the parameter num are the same, but they are two independent variables. Changing the value of the num parameter in the function does not affect the value of the variable count outside the function.
Therefore, the transmission of basic type value parameters of functions in JavaScript is passed by value.
Transfer of reference type value
function setName (obj) { obj.name = 'Nicholas';}var person = new Object();setName(person);console.log(person.name); //'Nicholas'
In the preceding example, the value of the person variable is passed to the obj parameter of the function. In this case, a name attribute is added to the obj parameter in the function, the object parameter of the function allows the person variable outside the function to obtain a name attribute. From the result, the parameter of reference type value 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 in the variable object can be seen as an actual address (or pointer) of the object in the memory ). After passing the parameter, the value of the obj parameter is also the address of the object in the memory. Therefore, the object referenced by the value of the obj parameter in the function is equivalent to the object referenced by the value of the person variable.
function setName (obj) { obj.name = 'Nicholas'; obj = new Object(); obj.name = 'Greg'; return obj;}var person = new Object();var result = setName(person);console.log(person.name); //'Nicholas'console.log(result.name); //'Greg'
If the parameter is passed by reference, in the above example, the function changes the object referenced by the value of the obj parameter, the object referenced by the person value of the corresponding variable also changes. Changing the function syntax may help you better understand the value-based transmission of parameters.
function setName () { var obj = arguments[0]; obj.name = 'Nicholas'; obj = new Object(); obj.name = 'Greg'; return obj;}
Although the values of the person and obj variables are the same object address in the memory, they are two independent variables. If you change the value of the obj parameter in the function and direct it to another object in the memory, the value of the variable "person" will not change, but will still point to the original object.
Therefore, parameters of reference type values of functions in JavaScript are passed by value.
Conclusion
The above is a detailed description of how JavaScript function parameters are transmitted. For more information, see other related articles in the first PHP community!