Home  >  Article  >  Web Front-end  >  About the difference between basic data types and reference data types in js

About the difference between basic data types and reference data types in js

一个新手
一个新手Original
2017-10-24 09:20:081588browse

Stack and heap
Stack is an automatically allocated memory space, which is automatically released by the system; while heap is dynamically allocated memory, and the size is variable and will not be automatically released.

Basic types and reference types

Basic types: simple data segments stored in stack memory, the data size is determined, and the memory space size can be allocated.

The basic data types include Undefined, Null, Boolean, Number and String, their values ​​are stored directly on the stack;

Reference type: the value of the object stored in the heap memory, represented by The address pointer is composed of a value. The address is stored in the stack, and the actual value is stored in the heap. The size of each space in the heap is different, and specific allocations are made according to the situation. When we need to access the value of a reference type (such as an object, array, function, etc.), we first obtain the address pointer of the object from the stack, and then obtain the required data from the heap memory;


 var s='str',t= 0,floatNum=1.1;
    var obj={
        a:10,
        b:'Joel',
        c:function(){
            console.log('function');
        }
    }

js 基本数据类型引用数据类型内存分配

As shown above:

The basic data type value is saved in the stack, the reference type object reference is saved in the stack, the value is Stored in the heap;

Summary

Different memory allocations when declaring variables:

Basic types: simple data segments stored in the stack, their values Store directly at the location where the variable is accessed. This is because the space occupied by basic types is fixed, so they can be stored in a smaller memory area - the stack, so that the value of the variable can be quickly queried.

Reference type: Object stored in the heap. The value stored in the stack is a pointer (point) used to point to the memory address of the stored object. This is because the size of the reference type value will change. , so it cannot be placed on the stack, otherwise it will reduce the speed of variable search. On the contrary, the value stored on the stack is the address of the object and the size of the address is fixed, so storing it on the stack has no impact on variable performance.

Different memory allocation mechanisms also bring different access mechanisms

In JavaScript, direct access to objects stored in heap memory is not allowed, so when accessing an object, first Access the memory stack to get the address of the object in the memory heap, and then follow this address to obtain the value in the object. This is the legendary access by reference, and the value of the basic type is directly in the memory stack.

Differences in copying variables

Basic types: When a variable holding the original value is copied to another variable, a copy of the original value will be assigned to the new variable. After that, the two The variables are completely independent, they just have the same value.

Reference type: When a variable holding the memory address of an object is copied to another variable, the memory address will be assigned to the new variable, which means that both variables point to the heap memory. For the same object, changes made by either of them will be reflected in the other. (One thing to understand here is that when copying an object, an identical object will not be created in the heap memory, but there will just be one more variable that holds the pointer to the object.) There is one more pointer.

Differences in parameter passing (the process of copying actual parameters to formal parameters)

The parameters of all functions in ECMAScript are passed by value, but why are primitive types and reference types involved? Is there still a difference when the value of ? It's not just because of the difference in memory allocation.
Basic data type: Just make a copy of the value in the variable and pass it to the parameter. After that, the parameter and the variable have no influence on each other.
Reference type: Because the value it passes is a memory address, they all point to the same object;

The above is the detailed content of About the difference between basic data types and reference data types in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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