Home > Web Front-end > JS Tutorial > Introduction to js basic data types and reference types, as well as deep and shallow copying and memory allocation issues

Introduction to js basic data types and reference types, as well as deep and shallow copying and memory allocation issues

巴扎黑
Release: 2017-09-04 10:24:45
Original
1355 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template