Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Parameter transfer in JS

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the case analysis of parameter transfer in JS". In the daily operation, I believe that many people have doubts about the case analysis of parameter transfer in JS. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "case analysis of parameter transfer in JS". Next, please follow the editor to study!

1. What is the value passing?

In the process of passing parameters to a function, the argument passes the value to the parameter.

EXP:

Function fun (x) {

Console.log (x)

}

Let a = 123

Fun (a)

Running result

In the function call statement fun (a), the argument is an and the parameter is x. From the output, it can be proved that argument a passes the value 123 to parameter x.

Question: can the value of parameter a be changed by changing the value of parameter x?

EXP:

Function fun (x) {

X = 666

}

Let a = 123

Fun (a)

Console.log (a)

Running result:

You can see that the value of argument a does not change due to the change of x. It is because of the characteristics of value transfer that we move on.

2. The characteristics of value transfer:

One-way transfer, only the value of the argument can be passed to the parameter, not the value of the parameter.

EXP:

We want to write a function swap that exchanges the values of two variables.

Function swap (x, y) {

Let t

T = x

X = y

Y = t

}

Let a = 123

Let b = 456

Swap (a, b)

Console.log (a, b)

Running result:

Although swap (aformab) is tuned, the value of the argument aformab does not change. It is because the actual parameter a _ memory b and the formal parameter x _ memory y are different spaces in memory. Here we introduce the concept of address.

An address is a number in memory, which is equivalent to what we often call a reference ID (reference ID is the optimized address).

You can think of memory as a tall building, so the address number is a room number in the building.

Let's simulate the exchange process of real participation parameters through memory. (as shown in the following figure) assume that the address of argument an is 18 and that of argument b is 19. The address of formal parameter x is 20 and the address of formal parameter y is 21.

So after the swap function is executed. The values of the formal parameters x and y are indeed exchanged, but because the shape participating parameters are in different spaces, the change of the formal parameter x and y can not affect the actual parameter a _ r _ b.

Question: is there any other way to change the value of the parameter through the parameter?

Yes, when the passed argument is a reference type, you can change the value of the space the argument points to through the parameter.

This sentence is more difficult to understand. Don't worry, let's look into the problem.

1. The difference between built-in basic types and reference types as arguments:

First of all, no matter what type of data the argument is, what the argument passes to the parameter must be the value of the argument itself.

From the swap function just now, we have actually come to a conclusion:

When the passed argument is a built-in basic type, the parameter cannot change the value of the argument.

What happens when the argument is reference type data?

EXP:

We still want to write a swap function with interchange capabilities, but this time the argument to the swap function is an array of reference type data. The exchange of elements within the array is realized through the swap function.

Let arr = [1,2]

Function swap (arr1) {

Let t

T = arr1 [0]

Arr1 [0] = arr1 [1]

Arr1 [1] = t

}

Swap (arr)

Console.log (arr [0], arr [1])

Running result:

This time we did exchange the values of arr [0] and arr [1] in the arr array.

The reason is that reference types are made up of two blocks of space in memory:

We still use memory to simulate how application type data is stored in memory, with 20 representing a piece of space and 18 representing a piece of space. As shown in the figure, 18% of the space is the space where the real data is stored (the heap space out of new), and 20 is the address of the space where the real data is stored.

When the swap function is called, the argument arr passes the value 18 (that is, the address of the new out of the space) to the formal parameter arr1. This means that they all point to the same space, so manipulating arr1 in the swap function is equivalent to manipulating arr itself. Just like a house, there are two keys, any key can open the house. So the values of the arr array are swapped.

Summary:

1. The parameters of JS can only be passed by value, and the essence of so-called reference transfer is value transfer.

two。 The value is passed one-way.

3. When the built-in basic type is used as an argument, the value of the argument cannot be changed through the parameter.

4. When the reference type is used as an argument, you can change the value of the space that the argument points to through the parameter.

Think: (if you have any questions, you are welcome to talk in private.)

Let arr1 = [1,2]

Let arr2 = [3,4]

Function swap (arr1, arr2) {

Let t

T = arr1

Arr1 = arr2

Arr2 = t

}

Swap (arr1, arr2)

Console.log (arr1, arr2)

At this point, the study of "instance Analysis of Parameter transfer in JS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report