The js function is passing a copy of the argument. Now let’s look at a concrete example.

Basic data types

let a = 123

let foo = (num) = > {
    num = 456;
}

foo(a)
console.log(a);
Copy the code



It makes sense that the function passes in a copy of a, so A doesn’t change.

Reference data type

let a = [1.2.3]

let foo = (obj) = > {
    obj[1] = 999
}

foo(a)
console.log(a);
Copy the code

A has changed. It looks like the address pointer to A has been passed in. Let’s do another example.

let a = [1.2.3]

let foo = (obj) = > {
    obj[1] = 999
    obj = [1.1.1]
    obj[0] = 111
}

foo(a)
console.log(a);
Copy the code



We found that when we point obj to a new object, a doesn’t change. This is a good illustration of the sentence at the beginning of the article, the transfer of reference is a copy. So when the argument is a reference data type, we pass in a copy of the pointer to the heap address of that reference data type, so now we have two Pointers to that object, and then we copy the pointer to a new object, and of course we don’t affect the original object.

Record the record!