JavaScript parameters are passed in what way?

1. The basic types of transfer mode

    <script>
        var a = 1;
        function test(x) {
            x = 10;
            console.log(x);
        }
        test(a); // 10
        console.log(a); // 1
    </script>

2. complex type passed by reference

    <script>
        var a = {
            a: 1,
            b: 2
        };
        function test(x) {
            x.a = 10;
            console.log(x);
        }
        test(a); // { a: 10, b: 2 }
        console.log(a); // { a: 10, b: 2 }    
    </script>

3. Press the share transfer
complex type This happens characteristic, the reason is in the transfer process, a first target to produce a copy of a,
this is not a copy of a copy of a deep cloned, a copy of the object a point to the same address pointing to heap memory.
Thus the modification in the body of the function x = 10 is only a copy of the modified, a target does not change. However, if the modification is a modification xa = 10 both point to the same pile of memory, which is the object will be a influences.

Some people say this feature is called passing references, there is a saying called the press share transfer

Guess you like

Origin www.cnblogs.com/wangxi01/p/11590207.html