Home > Web Front-end > JS Tutorial > body text

JS data type + reference type deep and shallow copy and memory allocation issues

php中世界最好的语言
Release: 2018-04-18 13:59:26
Original
1255 people have browsed it

This time I will bring you the data type of JS The problem of deep and shallow copying and memory allocation of reference types. What are the precautions when using JS data type reference type deep and shallow copying and memory allocation? , the following is a practical case, let’s take a look.

js deep and shallow copy problem

Shallow copy generally refers to basic type of copy

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 when you want to store json, you must use JSON.stringify() to convert json into string. Then use JSON.parse() to convert it into json data

Disadvantages: JSON.parse and JSON.stringify only support IE9 and above

To solve this problem, you can use the depth ratio copy method

Memory allocation issues in js (heap and stack)

Basic types in js are generally stored on 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 on the stack and the value is stored on 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 the object.)

2. The basic type is destroyed when the current execution environment ends, but the reference type will not be destroyed when the execution environment ends. This object will be recycled by the garbage collection mechanism only when all variables that reference it do not exist.

js basic data types and reference types

Basic data types include number boolean string undefined null symbol

Reference data type: array obj function

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:



The above is the detailed content of JS data type + reference type deep and shallow copy 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