Concept of memory
- Stack: Stacks store simple data types and provide an in-memory environment for the browser to run
- Heap: Stores complex data types, which can be understood as hard disks
JS data type
- Simple data types: string, number, Boolean, undefined, null
- Complex tree types: All objects are complex data types (arrays, functions).
Data creation process
Simple data creation process
- 1. Create a value (in the stack)
- 2. Declare a variable and pair it by context
- 3. Associate the created variable with the value
Complex data type creation process:
- 1. Create an object value, create a space in the heap to store the data, and have a hexadecimal address for the object
- 2. Declare a variable name (stored on the stack)
- 3. Associate variables in the stack with data in the heap
Depth copy:
Q: If a simple data type assigns values to each other and changes the value of one variable, does the value of the other variable change? A: No change, simple data types are stored on the stack, data replication is directly A separate copy of the value of one variable to another variable, there is no correlation between them, change the value of one variable, the other is not affected.
The default copy of a simple data type is a deep copy, and that’s why any API that operates on strings doesn’t change the constancy of strings
let a = 12; let b = a; console.log(a, b); // 12 12 b = 23; console.log(a, b); / / 12 23Copy the code
Q: When a complex data type assigns values to each other and changes the value of one variable, does the value of another variable change? A: complex data types in the stack are stored in the heap memory corresponds to an address, complex data variable copy each other is the stack in the address of the replication, the corresponding data in the heap is actually the same, so if after complex data types are copied, modified the value of A variable, the value of another variable is changing
Let c = {id: 0, age:12} let d = c; C. name = 'console.log' (c, d); //{name:' red ',age:12} {name:' red ',age:12}Copy the code
Complex data types are shallow-copied by default, and traversal is used if you want to implement deep copy
Note: Update to here first, then fill share content