Detailed description of the Javascript assignment mechanism, and javascript assignment
Today, I answered a question about Javascript, which involves the assignment problem. Therefore, I would like to summarize this question.
Copy codeThe Code is as follows:
Var a = 'test ';
Var B = function (){};
B. a = 'test ';
Function change (m, n ){
M = 'change ';
N. a = 'change ';
}
Change (a, B );
After the above code is executed, will the values of variables a and B change?
Original Value and reference value
The original values and reference values are introduced in previous articles. The original values refer to Undefined, Null, Boolean, Number, String, and so on. They are stored in the stack, the reference value is integrated from the Object, which is stored in the heap.
Here we need to distinguish the two:
Copy codeThe Code is as follows:
Var a = 'test ';
Var B = new String ('test ');
Var A = 'true ';
Var B = new Boolean ('true ');
In the preceding four variables, a and A are original values, while B and B are reference values.
Assignment Mechanism
After clarifying the differences between the original value and the reference value, we can introduce the Assignment Mechanism of Javascript:
In Javascript, a copy is generated for each assignment of a variable of the original value type. For a reference value, just as its name is, a value is assigned by reference, point to the memory of the same storage object.
Original Value assignment:
Copy codeThe Code is as follows:
Var a = 1; // Original Value
Var B = a; // generate a copy to variable B
B = 2; // irrelevant to
Alert (a); // output 1
Value assignment of reference values:
Copy codeThe Code is as follows:
Var A = new Object (); // reference value
A. x = 1;
Var B = A; // value assigned by reference, pointing to the same memory
B. x = 2; // modifying B will affect
Alert (A. x); // output 2
Parameter transfer
Now let's take a look at how to pass two types of values to the function parameters at any time.
1. Pass the original value
Copy codeThe Code is as follows:
Var a = 1;
Function test (m ){
M = 2;
}
Test ();
Alert (a); // output 1
The output is 1, so we know that the function only transmits the value of the variable, so m in the function body gets the value 1 and is assigned to 2, this process does not affect external variable.
2. Pass the reference value
Copy codeThe Code is as follows:
Var A = new Object ();
A. x = 1
Function test (M ){
M. x = 2;
}
Test ();
Alert (A. x); // output 2
The output is 2, so we know that the function has passed the variable address, so M in the function body gets the address passed, therefore, when attribute x is assigned 2 values, it also affects A pointing to the same memory address.
Summary
Now let's go back to the opening question:
Copy codeThe Code is as follows:
Var a = 'test ';
Var B = function (){};
B. a = 'test ';
Function change (m, n ){
M = 'change ';
N. a = 'change ';
}
Change (a, B );
Variable a is the original value, variable B is the reference value, and one is passed into the function as the value and the other is the address. Therefore, variable a does not change after the function is run, the value of variable B will change.