Passing parameters in JS syntax is a very important concept for beginners. Many friends will have a lot of trouble learning “value passing” and “reference passing”. Today we will analyze the value transfer in JS through various postures.

This article will use 10 minutes of time without dead Angle analysis JS parameter way, hope to help you.

JS only has value passing, no reference passing. This sentence may overturn some people’s cognition, but please don’t worry, soon you will agree with me.

1. What is value passing?

During function parameter passing, the argument passes the value to the parameter.

EXP:

function fun(x) {
    console.log(x);
}
let a = 123;
fun(a);
Copy the code

Running results;

In the fun(a) call statement, the argument is a and the parameter is x. From the output, we can see that the argument a passes the value 123 to x.

Question: Is it possible to change the value of argument A by modifying the value of parameter x?

EXP:

function fun(x) {
    x = Awesome!;
}
let a = 123;
fun(a);
console.log(a);
Copy the code

Running results:

You can see that the value of the argument a does not change as x changes. Because of the nature of value passing, let’s move on.

2. Characteristics of value transfer:

The value of an argument can only be passed to the parameter, but not to the parameter.

EXP:

We want to write a function swap that swaps 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);
Copy the code

Running results:

Swap (a, b) is called, but the values of the arguments a and b are not changed. This is because arguments A and b are in a different space in memory from parameters x and y. Here we introduce the idea of an address.

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

If you think of memory as a tall building, the address number is the number of a room in the building.

Let’s simulate the exchange of real participating parameters in memory. Assume that the address of argument A is 18 and the address of argument B is 19. The address of parameter x is 20, and the address of parameter y is 21.

So after swap is done. The values of x and y do swap, but since the parameters are in different Spaces, changes in x and y do not affect arguments A and b.

Question: Is there any other way to change the value of an argument using a parameter?

Yes. If the argument passed is a reference type, you can change the value of the space to which the argument points by using the parameter.

This sentence is more difficult to understand. Don’t worry, let’s get to the bottom of this problem.

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

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

From the swap function, we have already reached a conclusion:

When an argument is passed as a built-in primitive type, the parameter cannot change its value.

What happens when the argument is a reference type?

EXP:

We still want to write a swap function, but this time swap takes an array of reference data. Swap the elements of an array using swap.

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]);
Copy the code

Running results:

This time it does swap arr[0] and arr[1] in the arR array.

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

Let’s still use memory to simulate how application type data is stored in memory, with 20 representing a space and 18 representing a space. As shown in the figure, 18 is the real data storage space (new heap space),20 is the address of the real data storage space.

On the swap call, arr passes the value 18 (the address of the space new comes out of) to arr1. This means that they all point to the same space, so manipulating ARR1 in swap is equivalent to manipulating ARR itself. Just like a house, there are two keys, either key can open the house. So the arR array is swapped.

Conclusion:

  1. JS parameters only value pass, the so-called reference pass is the essence of value pass.

  2. Value passing is one-way.

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

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

Thinking :(if you have any questions, welcome to discuss privately)

    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);
Copy the code