JavaScript data manipulation _ talking about the operation essence of original value and reference value _javascript skill

Source: Internet
Author: User

my summary of the sentence: The original value, whether it is a variable assignment or a function pass, does not change the original value, the reference value whether it is a variable assignment or a function pass, and if the new variable is assigned, the original reference value is not affected, as if the new variable is a direct operation, it affects the original reference value.

First of all, the values and types are two different concepts. For example, NULL is a unique value for a null type, undefined is a unique value for the undefined type, and true and false are the only two values of the Boolean type. In any language, the operation of a value can be summed up in the following 3 areas.

Copy value: Assign value to new variable, or assign value to another variable, property, or array element by variable.

Passing value: The value is passed as an argument to a function or method.

Comparison value: That is, compare the value to another value to see if it is equal.

Because value-type data and reference-type data have different forms of values, the method of natural manipulation and the resulting results is also different. Note that when values are value type data, we often call them raw or base values, and when they are reference data, we often call them reference values or compound values.

1. Use original value

For the original value, the 3 levels of its operation are described below.

1) Copy Value

In an assignment statement, the process of an operation produces a copy of the actual value, and there is no connection between the value of the replica and the actual value, which is located separately in different stacks or heap areas. This copy can store the variables, the properties of the objects, and the elements of the array. For example:

var n = 123, A, B = [], c = {}; 
A = N;         Copy Digital 123 
b[0] = n;        Copy Digital 123 
c.x = n;        Copy Digital 123 
(A = = B[0]) && (a = = c.x) && (b[0] = = c.x) && alert ("Duplicate values are equal");  

In the example above, the value 123 is copied 3 copies to variable A, array B, and Object C, although their values are equal, but they are independent of each other.

2) Transfer value

When a value is passed to a function or method, the value passed is only a copy, not the value itself. For example, if you modify the passed in value in a function, the result can only affect the copy of the parameter value, and it will not affect the original value.

var a = 123;    The original value 

function f (x) { 
  x = x + x; 
} 
f (a);      Call function Modify passed value 

alert (a);   See if the value of variable A is affected and the return value is 123, which means no change

3) Comparison Value

In the example above we can also see that when comparing the original values, the byte-by comparison is used to determine whether they are equal. Comparisons are the values themselves, not the positions where the values are, although the results of comparisons may be equal, but only to indicate that they contain the same byte information.

2. Using reference values

For reference values, the 3 dimensions of their operation are described below.

1) Copy Value

In an assignment statement, the assigned value is a reference to the original value, not a copy of the original value, not the original value itself. In other words, after the assignment, the variable holds a reference to the original value (that is, the stored address of the original value). When you copy between multiple variables, array elements, or object properties, they are the same as the references saved by the original variable.

All references have the same effect and functionality and can perform operations, and if you edit the data through one of the references, the modification will be reflected in the original value and other relevant references. For example:

var a = [1,2,3];  The assignment array references 
b = A;         Copy value 
b[0] = 4;      Modifies the value of the first element in variable B, 
alert (a[0]);     

However, if you reassign the variable B to a new value, the new value does not affect the original value content. For example:

var a = [1,2,3];  The assignment array references 
b = A;         Copy value 
b = 4;         Override Assignment 
alert (a[0) for variable B;     

Duplicate assignment is actually a reference to the original value of the overridden variable, to a copy of another value, or to a reference to it. So there is no effect on the original value, the demo diagram is shown in Figure 4-2.

  

2) Transfer value

When passing data to a function using a reference, a reference to the original value is also passed to the function, which can be used to modify the original value itself, and any modifications are visible outside the function. For example:

var a = [1,2,3]; 
function f (x) { 
  x[0] = 4;   Modify the parameter value 
} 
f (a) in the function       ; Transitive reference value 
alert (a[0]);    

Notice that the reference to the external object or array is modified within the function, not the value of the object or the array itself. You can use a reference within a function to modify an object's properties or an array's elements, but if you overwrite the original reference with a new reference within the function, the modification within the function does not affect the value of the original reference, and is not visible outside the function.

var a = [1,2,3]; 
function f (x) { 
  x = 4;    Modify the parameter value 
} 
f (a) in the function       ; Transitive reference value 
alert (a[0]);    

3) Comparison Value

When two reference values are compared, two reference addresses are compared to see whether they refer to the same copy of the original value, rather than comparing their original byte for equality. When a reference is made to two different values, although they have the same byte composition, the values of the two references are not equal.

var a = new number (1);  Reference value a 
var b = new number (1);  Reference value B 
var c = A;        Assigning a reference to C 
alert (a==b);       Returns false 
alert (A==C);       

So {} = = {},[] = = [], returns false. Because the reference address is different.

In short, using values and using references are two basic methods of data manipulation for any language. When we manipulate the data, what methods are used to process, mainly look at the type of data. Value types and reference data participate in operations in different ways, value-type data manipulate data by using values, and reference data uses references to manipulate data. The different ways of operation, the results produced by nature are also different. Let's look at one more example:

var s = "abc";         String, value type data 
var o = new string (s);     String object, boxed string 


function f (v) {         //op function 
  v.tostring = function () {  ///Modify Parameters Method ToString () return 
    123 ; 
  }; 
} 

f (s);    Incoming value 
alert (s);  Returns the string "ABC", stating that the operation did not affect the original data 
F (o);    Incoming reference 
alert (o);  Returns a value of 123 that indicates that the operation has affected the internal structure of the original data

Value types are involved in an actual value and are therefore not directly related to the original data. The reference type, which participates in the operation of the reference address, results in an impact on the heap area data block associated with the reference address. However, with one exception, for JavaScript strings, it is more complex to manipulate, please google!

Above this JavaScript data operation _ talking about the original value and reference value of the operation of the essence of small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.