Two types:
The ecmascript variable contains two different types of values: basic type value and reference type value;
- Basic Type value: refers to the simple data segment stored in the stack memory;
- Reference Type value: refers to the objects stored in the heap memory. It means that the objects stored in the variable are actually just a pointer, and this pointer is executed in another location in the memory, stores objects at this location;
Two access methods:
- Basic Type value: access by value and operate on the actually saved values;
- Reference Type value: access by reference. When querying, We need to read the memory address from the stack first, and then find the value stored in the heap memory;
Two types of Replication
- Replication of basic type variables: When copying a variable to a variable, a new value is created in the stack, and then the value is copied to the location assigned to the new variable;
- Copy of reference type variables: Copies the pointer stored in the stack and copies the pointer to the space not allocated by new variables in the stack, the pointer copy and the original pointer execute the same object stored in the heap;
- After the copy operation, the two variables will actually reference the same object. Therefore, changing one of them will affect the other;
Function parameter transfer:
All function parameters in ECMA are passed by value;
This is easy to confuse, that is, parameters are all basic types, values, not objects.
1 function setName (obj) {2 obj.name = ‘Nicholas‘;3 obj = new Object();4 obj.name = ‘Greg‘;5 }6 7 var person = new Object();8 setName(person);9 alert(person.name); //"Nicholas"
Two types of Variables
Value Type and reference type