Home  >  Article  >  Web Front-end  >  Detailed explanation of how to pass JavaScript function parameters

Detailed explanation of how to pass JavaScript function parameters

黄舟
黄舟Original
2017-03-22 14:54:221460browse

This article mainly introduces the method of passing function parameters in JavaScript, which has a good reference value. Let’s take a look at it with the editor.

JavaScript uses a variableobject to track the lifetime of the variable. Basic type values ​​are stored directly in the variable object; while reference type values ​​are stored in the variable object as a pointer that points to the storage location of the actual object in memory.

Transfer of basic type values

When passing a basic type value to a parameter, the passed value will be copied to a local variable (named parameter, or arguments an element in the 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 function parameter num for use in the function. At this time, although the values ​​of the variable count and the parameter num are the same, But 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.

So the basic type value parameters of functions in JavaScript are passed by value.

Transfer 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, it is the parameter obj inside the function. Add a name attribute, and the function's parameter obj causes the variable person outside the function to also obtain a name attribute. From the results, it seems that the reference type value parameters of functions in JavaScript are 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. 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 parameter obj in the function is equivalent to the object referenced by the value of the operating 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, the function changes the object referenced by the value of parameter obj, then the object referenced by the value of the corresponding variable person will also change. Changing the way the function is written may be more helpful in understanding the passing of parameters by value.

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

Although the values ​​of variable person and parameter obj are the same object's address 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 the memory, the value of the variable person will not change and will still point to the original object.

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

in conclusion

The above is the detailed content of Detailed explanation of how to pass JavaScript function parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn