In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of JavaScript basic data types", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of JavaScript basic data types".
Basic data structure
Stack
Stack, a linear table that only allows inserts or deletions in one segment, is a first-in-and-out data structure.
Heap
Heap is a data structure based on hashing algorithm.
Queue
A queue is a first-in, first-out (FIFO) data structure.
Storage of data types in JavaScript
In JavaScript, data types are divided into basic data types and reference data types, and one difference between them is that they are stored in different locations.
Basic data type
We all know that the basic data types in JavaScript are:
String
Number
Boolean
Undefined
Null
Symbol (for the time being)
The basic data types are simple data segments that are stored in stack memory.
Reference data type
The reference data types in JavaScript are:
Array
Object
The reference data type is saved in heap memory, and then a reference to the actual object in heap memory is saved in stack memory. Therefore, operations on reference data types in JavaScript are references to operands rather than actual objects.
It can be understood that there is an address in the stack memory that is related to the actual value in the heap memory.
Graphic illustration
Now, let's declare a few variables to try:
Var name= "axuebin"; var age=25;var job;var arr= [1J 2je 3]; var obj= {age:25}
You can use the following figure to show how data types are stored in memory:
At this time, the three basic data types of name,age,job are directly stored in stack memory, while arr,obj only stores an address in stack memory to represent references to heap memory.
Copy
Basic data type
For basic data types, if you copy, the system automatically assigns a new value to the new variable in the stack memory, which is easy to understand.
Reference data type
For reference data types such as arrays and objects, there is a difference when copying:
The system also automatically assigns a value to the new variable in the stack memory, but this value is only an address. That is, the copied variable has the same address value as the original variable, pointing to the same object in the heap memory.
As shown, after var objCopy=obj is executed, obj and objCopy have the same address value and execute the same actual object in heap memory.
What's the difference?
When I modify obj or objCopy, it causes another variable to change.
Why?
Why is the underlying data type in the stack and the reference data type in the heap?
The stack is larger than the stack, and the stack is faster than the alignment.
The basic data type is relatively stable and takes up relatively little memory.
The reference data type size is dynamic and infinite.
Heap memory is unordered storage and can be obtained directly based on references.
Reference article
Understand js memory allocation
Original and reference values
In ECMAScript, variables can hold two types of values, the original value and the reference value.
The original value refers to the value that represents the original data type (basic data type), that is, the value represented by the Undefined,Null,Number,String,Boolean type.
Reference values refer to values of composite data types, namely Object,Function,Array, custom objects, and so on.
Stack and heap
There are two kinds of memory structures corresponding to the original value and the reference value, namely stack and heap.
Stack is a last-in-first-out data structure. The behavior of stack can be simulated by Array in javascript.
The original values are simple data stored in the stack, that is, their values are stored directly in the location where the variable is accessed.
Heap is a data structure based on hash algorithm. In javascript, reference values are stored in the heap.
The reference value is the object stored in the heap, that is, the value stored in the variable (that is, the variable pointing to the object, stored in the stack) is a pointer to the actual object stored in the heap.
Example: var obj = new Object (); obj is stored in the stack which points to the object new Object (), while new Object () is stored in the heap.
So why are the reference values on the heap and the original values on the stack, not all in memory? why not put them together? So next, let's explore the answer to the question!
First, let's take a look at the code:
Function Person (id,name,age) {this.id = id;this.name = name;this.age = age;} var num = 10There var bol = true;var str = "abc"; var obj = new Object (); var arr = ['axiajiajie']; var person = new Person (100, "motto of fools", 25)
Then let's take a look at the memory analysis diagram:
Variables num,bol,str are basic data types, their values are stored directly in the stack, obj,person,arr is a compound data type, and their reference variables are stored in the stack, pointing to the actual objects stored in the heap.
As can be seen from the picture above, we cannot directly manipulate the data in the heap, that is, we cannot directly manipulate the object, but we can manipulate the object through the reference to the object in the stack, just as we operate the TV through the remote control machine. The difference is that the TV itself does not have a control button.
Now let's answer the question of why the reference value should be placed in the heap and the original value in the stack:
Remember one sentence: energy is a matter of balance, it's just a matter of time for space and space for time.
The stack is larger than the stack, the operation speed of the stack is faster than the stack, the object is a complex structure, and can be expanded freely, for example, the array can be expanded infinitely, and the object can add properties freely. They are placed in the heap so as not to affect the efficiency of the stack. Instead, the actual object in the heap is found by reference and then operated. Compared to simple data types, simple data types are more stable and occupy only a small amount of memory. Simple data types are not placed in the heap because it takes time to find the actual object by reference to the heap, and the overall cost is much greater than the cost of getting the actual value directly from the stack. So the values of simple data types are stored directly on the stack.
The above is all the content of the article "sample Analysis of data types based on JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.