Su-MO Orange
Links: https://www.zhihu.com/question/27114726/answer/35481766
Source: Know
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Before the first time to see this part of the time is also a bit foggy, today saw the subject asked this question and carefully looked again, consulted the relevant information, now is more clear.
Combine your own understanding to sort it out.
Because to be completely clear about this problem involves some more in-depth knowledge points, so the length is longer, hope to read patiently.
In fact, you just have to figure it out: Save the object's variable, which contains the value of the object's address in the heap memory.
The following is a detailed breakdown:
Data type
The data types in JavaScript can be divided into two categories:
- The original data type value primitive type, such as undefined,null,boolean,number,string.
- A reference type value , that is, an object type, objects, such as Object,array,function,date.
Different memory allocations when declaring variables
- Raw values: Simple data segments stored in stacks (stack), that is, their values are stored directly in the location where the variable is accessed . This is because the space occupied by these primitive types is fixed, so they can be stored in a smaller memory area – the stack. This stores the values that facilitate quick lookup of variables.
- Reference value: The object stored in the heap (heap), that is, the value stored at the variable is a pointer (point) to the memory address of the storage object. This is because the size of the reference value will change, so it cannot be placed on the stack, otherwise it will reduce the speed of the variable search. Instead, the value placed in the stack space of the variable is the address that the object stores in the heap. The size of the address is fixed, so storing it in the stack has no negative effect on the performance of the variable.
Different memory allocation mechanisms also bring different access mechanisms
In JavaScript, it is not allowed to directly access objects stored in the heap memory, so when accessing an object, we first get the address of the object in the heap memory, and then follow this address to get the value of this object, which is the legendary access by reference . The value of the primitive type is directly accessible.
Different when copying variables
- Original value: When you copy a variable that holds the original value to another variable, the copy of the original value is assigned to the new variable, and then the two variables are completely independent, they just have the same value.
- Reference value: When you copy a variable that holds an object's memory address to another variable, the memory address is assigned to the new variable, which means that the two variables point to the same object in the heap memory, and any change they make will be reflected on the other. (one thing to understand here is that copying an object does not make an identical object in the heap memory, just a variable that holds pointers to the object)
Differences in Parameter passing
First we should make it clear that the parameters of all functions in ECMAScript are passed by value . But why is there still a difference when it comes to the value of the original type and the reference type, not just because of the difference in memory allocation. (I've compared this to the exact same mechanism that is used to copy variables, and you can simply understand that when you pass a parameter, it's the process of copying the argument to the formal parameter.)
- Original value: Simply pass the value in the variable to the parameter, and then the parameter and the variable do not affect each other.
Reference value: Object variable The value inside it is the memory address of the object in the heap memory, which you should always keep in mind! So the value that it passes is the memory address, which is why the changes to this parameter inside the function are external, because they all point to the same object. Maybe after I say this, you still have a little bit of understanding of the examples in the book, so please take a look at the picture:
So, if it is passed by reference, the contents of the second lattice (i.e. the variable itself) are passed in (there will be no fourth lattice). But the fact is that the variable passes the value inside it (copied) to the parameter, so that the parameter also points to the original object. So if you assign this parameter to another object inside the function, this parameter will change its value to point to the new object's memory address, but at this point the original variable still points to the original object, when they are independent of each other, but if this parameter is to change the properties inside the object, this change will be reflected in the external , because the object they are pointing to has been modified! Let's take a look at the following example: (Legendary call by sharing)
VarObj1={Value:' 111 '};VarObj2={Value:' 222 '};functionChangestuff(Obj){Obj.Value= ' 333 ' obj = obj2return obj. Value;} var foo = changestuff (obj1console. Log (fooconsole. (obj1. Value); //' 333 '
The comments in code are so underestimated that I moved here:
/*
Obj1 still points to the original object, the reason that value changed, * is because the first statement in Changestuff, this time obj is pointing to obj1. * Again, if it is passed by reference, this time obj1.value should be equal to ' 222 ' 's *
/
All right, here's the whole explanation of the problem.
If the JavaScript pass parameter is an object, is it passed by value or by reference?