Detailed Explanation of Transfer Mode of JavaScript Function Parameters

  • 2021-07-26 06:47:55
  • OfStack

JavaScript uses a variable object to track the lifetime of a variable. Primitive type values are stored directly in variable objects; The reference type value is stored in the variable object as a 1 pointer to where the actual object is stored in memory.

Passing of primitive type values

When you pass a primitive type value to a parameter, the passed value is copied to a local variable (that is, a named parameter, or an element in an arguments object).


function addOne (num) {
 num++;
 return num;
}
var count = 1;
var result = addOne(count);
console.log(count); //1
console.log(result); //2

In the above example, the value of the variable count is passed to the parameter num of the function for use in the function. Although the values of the variable count and the parameter num are identical, 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 basic type value parameters of functions in JavaScript are passed by value.

Passing of reference type values


function setName (obj) {
 obj.name = 'Nicholas';
}
var person = new Object();
setName(person);
console.log(person.name); //'Nicholas'

In the above example, the value of the variable person is passed to the parameter obj of the function. At this time, an name attribute is added to the parameter obj inside the function, and the function obtains an name attribute to the variable person outside the function for the parameter obj. As a result, the reference type value parameter 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 in the variable object can be regarded as the address (or pointer) of an actual object in memory. Since the value of the parameter obj after the parameter is passed is also the address of the object in memory, the object referenced by the value of the operation parameter obj in the function is equivalent to the object referenced by the value of the operation variable person.


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, if the function changes the object referenced by the value of the parameter obj, the object referenced by the corresponding variable person will also change. Changing the writing of the function may be more helpful to understand the value-by-value passing of parameters.


function setName () {
 var obj = arguments[0];
 obj.name = 'Nicholas';
 obj = new Object();
 obj.name = 'Greg';
 return obj;
}

Although the value 1 of the variable person and the parameter obj are both the address of the same object in memory, they are two independent variables. If you change the value of the parameter obj in the function so that it points to another object in memory, the value of the variable person will not change and will still point to the original object.

Therefore, the reference type value parameters of functions in JavaScript are passed by value.

Conclusion

Arguments to all functions in JavaScript are passed by value.


Related articles: