My summary of one sentence: The original value will not change the original value regardless of whether it is a variable assignment or a function pass. Whether it is a variable assignment or a function pass, if the new variable is reassigned, it will not affect the original reference value. If the new variable is a direct operation, it will affect the original reference value.
First of all, it is clear that values and types are two different concepts. For example, null is the unique value of null type, undefined is the unique value of undefined type, and true and false are the only two values of boolean type, etc. In any language, the operation of value can be summarized into the following 3 aspects.
Copy value: that is, assign the value to a new variable, or assign the value to another variable, attribute or array element through the variable.
Passing value: that is, passing the value as a parameter to a function or method.
Compare values: that is, compare the value with another value to see if it is equal.
Since the values of value type data and reference data have different forms, the methods of operating them naturally and the results produced are also different. Note that when the value is value-type data, we often call it the original value or basic value; when the value is referenced data, we often call it the referenced value or composite value.
1. Use original values
For the original value, the three levels of its operation are explained as follows.
1) Copy the value
In the assignment statement, the operation process will produce a copy of the actual value. There is no connection between the value of the copy and the actual value. They are located in different stack areas or heap areas alone. This copy can store variables, objects' properties, and array elements. For example:
var n = 123, a, b = [], c = {}; a = n; // Copy the number 123 b[0] = n; // Copy the number 123 cx = n; // Copy the number 123 (a == b[0]) && (a == cx) && (b[0] == cx) && alert("The values copied are equal"); // Detection that their values are equalIn the above example, the value 123 is copied 3 copies to the variable a, array b and object c respectively. Although their values are equal, they are independent of each other.
2) Pass the value
When passing a value to a function or method, the passed value is only a copy, not the value itself. For example, if you modify the passed value in a function, the result can only affect the copy of this parameter value and will not affect the original value.
var a = 123; // The original value function f(x){ x = x + x; } f(a); // Call the function to modify the passed value alert(a); // Check whether the value of variable a is affected. The return value is 123, indicating that there is no change3) Comparison value
In the example above we can also see that when comparing the original values, a byte-byte comparison is performed to determine whether they are equal. Comparing the values themselves, not the location where the values are. Although the results of the comparison may be equal, it only means that the byte information they contain is the same.
2. Use reference values
For reference values, the three levels of their operations are explained as follows.
1) Copy the value
In the assignment statement, the assigned value is a reference to the original value, not the original value copy, nor the original value itself. That is to say, after the assignment is performed, the variable saves references to the original value (that is, the storage address of the original value). When copied between multiple variables, array elements, or object properties, they will all be the same as the references saved by the original variable.
All references have the same effect and function and can be performed. If the data is edited through one of the references, this modification will be reflected in the original value and other related references. For example:
var a = [1,2,3]; // Assign array reference b = a; // Copy value b[0] = 4; // Modify the value of the first element in the variable b alert(a[0]); // Return 4, displaying that the value of the first element in the variable a is also modified to 4
However, if a new value is reassigned to variable b, the new value will not affect the content of the original value. For example:
var a = [1,2,3]; // Assign array reference b = a; // Copy value b = 4; // Overwrite the assignment alert(a[0]); // The content of variable a remains unchanged
Repeated assignments are actually overriding a variable's reference to the original value, becoming a copy of another value or a reference to it. Therefore, it will not affect the original value, and the demonstration diagram is shown in Figure 4-2.
2) Pass the value
When a reference is used to pass data to a function, the passed to the function is also a reference to the original value. The function can use this reference to modify the original value itself, and any modification is visible outside the function. For example:
var a = [1,2,3]; function f(x){ x[0] = 4; // Modify parameter value in the function} f(a); // Pass the reference value alert(a[0]); // Return 4, the original value changesNote that what is modified inside the function is a reference to an external object or array, not the value of the object or array itself. You can use a reference to modify the attributes of an object or the elements of an array within the function, but if a new reference is used inside the function to overwrite the original reference, then the modification inside the function will not affect the value of the original reference and cannot be seen outside the function.
var a = [1,2,3]; function f(x){ x = 4; // Modify parameter value in the function} f(a); // Pass the reference value alert(a[0]); // Return 1, the original value will not change3) Comparison value
When comparing two reference values, the two reference addresses are compared to see if the original values they reference are the same copy, rather than whether their original values are equal bytes. When referencing 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; // Assign a reference to c alert(a==b); // Return false alert(a==c); // Return true
So, {} == {}, [] == [], all return false. Because the reference address is different.
In short, for any language, using values and using references are two basic methods of data manipulation. When we operate data, what method should be used to process it mainly depends on the type of data. The value type and reference data participate in operations are different. Value type data operates on data by using values, while reference data operates on data using references. Different calculation methods also produce different results naturally. Let's take a look at another example:
var s = "abc"; // String, value type data var o = new String(s); // String object, boxed string function f(v){ // Operation function v.toString = function(){ // Method toString toString() return 123; }; } f(s); // Pass the value alert(s); // Return the string "abc", indicating that the operation has not affected the original data f(o); // Pass the reference alert(o); // Return the value 123, indicating that the operation has affected the internal structure of the original dataThe value type participates in the operation with the actual value, so it has no direct connection with the original data. The reference type participates in the operation with the reference address, and the calculation result will affect the heap area data block associated with the reference address. However, there is one exception. For JavaScript strings, its operation methods are relatively complicated. Please google for details!
The above article JavaScript data operation_A brief discussion on the essence of the operation of original values and reference values is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.