Value and reference

Quote "You do not know JavaScript" value and reference volumes 2.5

In many programming languages, assignment and parameter passing by value copy (value-copy) or a reference copy (reference-copy) to complete, depending on what we use grammar.

For example, in C ++ If you want to pass a digital function and change its value in the function, so you can declare parameters int & myNum, that is, if you pass the variable is x, myNum is a reference point x. A special reference is like a pointer to a pointer to a variable (alias). If you do not declare a reference parameter, then the parameter values ​​are always passed by way of copying the value is even too complex object values.

JavaScript is not a pointer, the working mechanism of references are not the same. In JavaScript variable to another variable can not be a point of reference.

JavaScript reference point is the value. If a value of 10 has references, which are directed to the same value, no reference / point between their mutual relationship.

JavaScript is no difference in syntax for assignment / transfer value and reference entirely determined according to the type of value.

Let's look at an example:

  a 2 = var;
  var B = a; // B is a copy of a value of ++ B;
  a; // 2
  B; //. 3

  C = var [1,2,3];
  var C = D; // D is [1,2,3] a reference d.push (. 4);
  C; // [1,2,3,4]
  d; // [1,2,3,4]

Simple values ​​(i.e. primitive values ​​scalar, scalar primitive) is always assigned a value by way of replication / transmission, including null, undefined, strings, numbers, Boolean and ES6 the symbol.

Composite values ​​(compound value) - objects (including arrays and packaging objects, Chapter 3) and the function, to always assign / transfer replication by reference.

In the embodiment 2 is a basic type of scalar values, a variable holding a copy of the value, b holding another copy of it. When b changes the value of a remains unchanged.

c and d are each directed to a different reference value of the same compound [1,2,3] two. Please note, c and d are merely point to the value of [1,2,3], it does not hold. So they change to the same value (such as calling .push (4)), and then they all point to the new changed value [1,2,3,4].

As the reference point is the value itself, not variable, so a reference can not be changed to another reference point.
     There are a = [1,2,3];
     var b = a;
     a; // [1,2,3]
     b; // [1,2,3]
     // then
     b = [4,5,6]; a; // [1,2,3] b; // [4,5,6]

b = [4,5,6] does not affect the value of a point [2,3], unless b is not a reference to an array, but a pointer pointing to, but there is no such case in JavaScript!

Arguments people often have such confusion:
      function foo(x) {
         x.push( 4 );
         x; // [1,2,3,4]

       // then
       X = [4,5,6]; x.push (. 7);
       X; // [4,5,6,7]

    }
    Var A = [l, 2,3];
    foo (A);
    A; // is [1,2,3,4], instead of [4,5,6,7]

When we pass to a function, the actual assignment is a copy of a reference to x, and a still point [1,2,3]. In function we can change the value of the array by reference x (after push (4) becomes [1,2,3,4]). But x = [4,5,6] does not affect a point, it is a still point [1,2,3,4].

We can not be changed by reference x references a point, can only change the values ​​of a and x common points.

Guess you like

Origin www.cnblogs.com/PasserByOne/p/12296649.html