Home > Article > Web Front-end > Introduction to js basic data types and reference types, as well as deep and shallow copying and memory allocation issues
The following editor will bring you a brief discussion of js basic data types and reference types, deep and shallow copy issues, and memory allocation issues. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
js Deep and shallow copy problem
Shallow copy generally refers to the copying of basic types
Deep copy generally refers to the copy of the reference type, and also copies the value of the reference type
Example
h5’s sessionStorage can only store strings, so it must be stored When using json, you must use JSON.stringify() to convert json to string, and then use JSON.parse() to convert json data
Disadvantages: JSON.parse and JSON. stringify only supports IE9+ and above
To solve this problem, you can use the depth ratio copy method
js Memory allocation issues (heap and stack)
Basic types in js are generally stored in the stack. These types each occupy a fixed size of space in the memory, and their values are stored in the stack space, which we access by value.
Reference types are data of variable size, but their address size is fixed, so the address is stored in the stack and the value is stored in the heap
1. Stack memory: stores basic types. Heap memory: stores reference types (store a basic type value in the stack memory to save the address of the object in the heap memory, which is used to reference this object.)
2. The basic type is destroyed when the current execution environment ends. The reference type will not be destroyed when the execution environment ends. The object will be recycled by the garbage collection mechanism only when all variables that refer to it no longer exist.
js basic data types and reference types
Basic data types include number boolean string undefined null symbol
Reference data type: array obj function
The above is the detailed content of Introduction to js basic data types and reference types, as well as deep and shallow copying and memory allocation issues. For more information, please follow other related articles on the PHP Chinese website!