When on the JS function parameter passing: the value of the difference between pass and passed by reference

When on the JS function parameter passing: the value of the difference between pass and passed by reference

JS and parameter passing is divided into two, one is passed by value, another passed by reference, but its essence is passed by value.

Value transfer : one-way transmission value is transmitted only by a real parameter arguments to, but not by the parameter passed argument. In memory, the real parameter participating in different units. The following diagram, for example, a, b values passed to the x, y, although they value the same, but they are not the same thing. This is the same with the person's name, you can call so and so, he can also be called so and so, but you two are not the same person. After the operation, the value of x and y are exchanged, but when the called function fn been executed, the parameter destruction occurs, but the calling function a, b does not affect, the final output 123,456.
Here Insert Picture Description

Passed by reference : is a reference ID, the equivalent number in memory address. The following diagram, for example, there is a space ARR, but it is not kept inside 1 and 2, 2 and 1, and another space is kept inside ARR points 1 and 2 where the space number. 1 and 2 where the space is opened up his own memory, the equivalent of a house, but space is equivalent to a key arr. Reference transmission value is transmitted biggest difference reference space has two types, a stored ID number is referenced, the other stored value is true.

Values ​​are transmitted in two operating space houses two equivalent; and a space is passed by reference operation, equivalent to a house two keys, so that the exchange can occur.

Summary : the difference between a reference value and pass pass:
1. For numeric data type, parameter values can not change the argument in the calling function.
2. For reference types, can change the value of the reference arguments in the calling function by forming.

Published an original article · won praise 0 · Views 12

Guess you like

Origin blog.csdn.net/weixin_46378811/article/details/104435538