Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript parameter passing by value and reference passing usage examples

伊谢尔伦
Release: 2017-07-26 17:09:22
Original
1214 people have browsed it

What is passing by value?

In other words, copying the value outside the function to the parameter inside the function is the same as copying the value from one variable to another variable.

Pass by value

A simple example:


##

var value = 1;
function foo(v) {
  v = 2;
  console.log(v); //2
}
foo(value);
console.log(value) // 1
Copy after login

It’s easy to understand, when passing value Entering function foo is equivalent to copying a copy of value. Assume that the copied copy is called _value. What is modified in the function is the value of _value without affecting the original value.

Passing by reference

Although copying is easy to understand, when the value is a complex data structure, copying will cause performance problems.

So there is another way of passing called passing by reference.

The so-called passing by reference means passing the reference of the object. Any changes to the parameters inside the function will affect the value of the object, because both refer to the same object.

For example:


var obj = {
  value: 1
};
function foo(o) {
  o.value = 2;
  console.log(o.value); //2
}
foo(obj);
console.log(obj.value) // 2
Copy after login

Is this passing by reference?

The third delivery method

Don’t worry, let’s look at another example:


var obj = {
  value: 1
};
function foo(o) {
  o = 2;
  console.log(o); //2
}
foo(obj);
console.log(obj.value) // 1
Copy after login

If JavaScript uses pass-by-reference, the outer value will also be modified. Why hasn't it been modified? So is it really not pass by reference?

This is about mentioning that there is actually a third delivery method, which is called shared delivery.

Shared transfer means that when transferring an object, a copy of the object's reference is transferred.

Note: Passing by reference is passing a reference to the object, while passing by sharing is passing a copy of the reference of the object!

So if you modify o.value, you can find the original value through reference, but modifying o directly will not modify the original value. So the second and third examples are actually passed by sharing.

Finally, you can understand it this way:

If the parameter is a basic type, it is passed by value, if it is a reference type, it is passed by sharing.

But because the copy is also a copy of the value, it is also directly considered to be passed by value in the elevation.

The above is the detailed content of Detailed explanation of JavaScript parameter passing by value and reference passing usage examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!