The target
- The ability to name memory allocations for simple data types
- Memory allocation capable of telling complex data types
- Can you tell how simple types pass parameters
- Can you tell how complex types are passed
directory
- Simple versus complex types
- Stack and heap
- Simple types of memory allocation
- Memory allocation for complex types
- Simple type parameter passing
- Parameter transmission for complex types
1. Simple and complex types
Simple types are also called primitive data types or value types, and complex types are referred to as reference types.
Value types: simple data types/basic data types. When stored, the variable stores the value itself, so they are called string, number, Boolean, undefined, null
Null is an empty object
If I have a variable that I don’t know what to put in it, I use null, okay
2. Stack and heap
Stack space allocation difference:
- Stack (operating system) : The operating system automatically allocates and releases storage function parameter values, local variable values, etc. It operates like a stack in a data structure; Simple data types are stored on a stack
- Heap (operating system) : Stores complex types (objects), which are usually allocated and released by the programmer, or collected by the garbage collection mechanism if the programmer does not release them.
Complex data types are stored in the heap
3. Simple types of memory allocation
Just put it on the stack
4. Memory allocation of complex types
First find the storage address in the stack (hexadecimal address), follow the address to find data in the heap
5. Simple type parameter transmission
A function parameter can also be considered a variable. When we pass a value variable as a parameter to a function parameter, we are actually copying the value of the variable in the stack space to the parameter, so that any changes made to the parameter inside the method will not affect the external variable.
Function fn (a) {a++; Console. log (a); } var x = 10; Fn (x); Console. log (x);Copy the code
The school asks you to hand in your ID card information, so you make a copy of the same one to the school. Your age is X =10 years old, but you find that you made a mistake and wrote a=11 years old. Finally, the information you promised is A =11 years old, but your actual age is still X =10 years old
6. Complex type parameter transmission
A parameter of a function can also be treated as a variable. When we pass a reference to a type variable to a parameter, we are actually copying the stack address of the variable to the parameter. The parameter and the argument actually hold the same heap address, so we operate on the same object.
Somebody wants to see me writing a complicated one. This one’s a little hard.