This article describes the relationship between javascript variables, values, addresses, and parameters. For more information, see the following:
1. The quocrip variable contains two types of values: A value of the reference type and a value of the basic type. The reference types include Array, Object, and Function (which can be understood as follows, all non-basic types are reference types). The five basic types include undefined, null, string, boolean, and number.
2. The function parameter transmission mechanism is to copy the variable value.
The book says, "copying the external value of the function to the parameter inside the function is the same as copying the value from one variable to another. The transfer of the basic type is the same as that of the basic type variable, while that of the reference type is the same as that of the reference type variable. "
When a variable copies a value of the reference type, it also copies the value stored in the variable object to the space allocated for the new variable. The difference is that the copy of this value is actually a pointer, and this pointer points to an object stored in the heap. After the copy operation is complete, the two variables actually reference the same object. Therefore, changing one of the variables will affect the other. "
[Note: copying the value of the reference type is the transfer address]
3. the parameter is actually a local variable of the function.
----------------------------------------------------------------------------
Basic concepts:
Pass value: pass the value of a to B, change B, and A will not change. B stores the same value as;
Address Transfer: transfers the address of A to B, changes B, and changes A at the same time. B only saves the address of A (similar to the computer shortcut ).
A data with value type is stored in a variable in the stack. That is, allocate memory space in the stack and store the included values directly. The values represent the data itself. Data of the value type has a fast access speed.
A data with reference type does not reside in the stack, but is stored in the heap. That is, allocate memory space in the heap. Instead of directly storing the contained values, it points to the value to be stored. The value indicates the address to which it points. When accessing a data with reference type, you need to check the content of the variable in the stack. The variable references an actual data in the heap. Data of the reference type has a larger storage scale and a lower access speed.
----------------------------------------------------------------------------
The following are three questions.
[Question 1 ]:Why is a not disturbed after the change (a) function is executed?
Script var a = [1, 2, 3]; function change (a) {console. log (a); // [1, 2, 3] a = 2; // set the value to console. log (a); // 2} change (a); console. log (a); // [1, 2, 3] script
Question 1: Because the execution process of change (a) is like this, object a (array) is first passed into change and then copied to parameter a of change. Then a = 2 is a value assignment statement, which is used to pass the value. At this time, a = 2 is a value type and does not involve the reference address. Therefore, external a is not affected.
[Question 2 ]:Why is a outside affected after the change (a) function is executed?
Script var a = [1, 2, 3]; function change () {a = 2; // transfer value} change (); console. log (a); // 2 script
Question 2: When change () is executed, the function searches for the scope chain in its own execution environment. The activity object does not contain variable, so I searched for the global execution environment along the scope chain and found the variable a. At this time, the and external a in the function were the same address in the memory, and the inside the function naturally changed, the external will also change.
Resolution: The difference between Question 2 and question 1 is that question 2 does not introduce parameters, so it does not involve copying variables.
[Question 3 ]:Why is a outside affected after the change (a) function is executed?
《script》 var a = [1, 2, 3]; function change(b) { b[0] = 2; } change(a); console.log(a); //[2,2,3]《script》
Question 3: This is very similar to Question 1. The only difference is that a = 2 is changed to B [0] = 2. I was confused at the beginning, not to mention copying? The parameter B should be a copy value. How can it affect the external?
Indeed, when the change function is executed, parameter B is the copy value of. Because a is a reference type, B and a access an address object by reference in the function. B [0] = 2 does not affect that B and a reference the same object in the function.
[Question 4 ]:Why is a not disturbed after the change (a) function is executed?
var a = [1, 2, 3]; function change(b) { console.log(b);//[1,2,3] b=2; b[0] = 2; } change(a); console.log(a); //[1,2,3]
Question 4: The change (B) Execution Process is like this. Object a passes in the change function and copies the value and address to object B. B = 2. At this time, B becomes a value type and does not involve address reference. Then, B [0] = 2 is actually meaningless, because B is no longer an array at this time, it naturally does not have the index method such as B [0. Therefore, the address reference relationship between B and a disappears after B = 2. At this time, the external a is still [1, 2, 3];
The above is all the content of this article. I hope you will like it.