In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how JavaScript allocates the memory of variables. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
All variables (including functions) of JavaScript are stored in memory throughout the processing, so one variable is processed. First, you have to allocate memory for the variable. JavaScript memory allocation, like other languages, allocates memory based on the data type of the variable, while the data type of the JavaScript variable is determined by the type of value assigned. The data types supported by JavaScript can be divided into two main categories: basic data types and complex data types. Basic data types include numeric (number) type, string (string) type, boolean (boolean) type, undefined (undefined) type, and null (null) type; complex types contain object (object) types, and arrays and functions belong to object types in JavaScript. All data types except basic data types are object types. In JavaScript, basic data type variables are allocated in stack memory, in which the values of variables are stored and accessed by value, while variables of object type allocate stack memory and heap memory at the same time, where stack memory stores addresses. The heap memory holds the referenced value, and the address of the stack memory points to the value of the heap memory. The access to the variable is accessed by reference, that is, the address where the stack memory is stored is read first, and then the value read by the heap memory is found by the address.
The main reason why JavaScript allocates memory according to different data types of variables is that stack memory is smaller than heap memory, and the size of stack memory is fixed, while the size of heap memory can be changed dynamically. The values of basic data types are fixed and the values of object types are not fixed, so it is reasonable to store them in stack memory and heap memory respectively.
Function Student (id,sno,name,age) {/ / function definition, Student is a function variable this.id = id; this.sno = sno; this.name = name; this.age = age;} var num = 20; / / num is a numeric variable var bol = false; / / bol is a Boolean variable var str = "student"; / / str is a string variable var obj = {} / / obj is an object variable var arr = ['axiomanageme']; / / arr is an array variable var student = new Studenta (1, "199706010016", "maomin", 23); / / student is an object variable
The Student variable in the above code defines a constructor, and the definition code of the function is stored in heap memory, and the corresponding address of that memory is stored in the Student function variable. The constructor is used to create an object instance, and the last line of code uses this constructor to create a student object instance named maomin. When the student instance is created, it returns its assigned address in heap memory, which is assigned to the student variable.
{} in the above code creates an empty object in heap memory whose address in heap memory is assigned to the variable of obj. ] is an array object with an element value of 'axiom memory', which is also stored in heap memory, and its corresponding address is assigned to the arr variable. In addition to Student, obj, arr, and student, which are object variables, several other variables num, bol, and str are basic data types, so they are all stored in stack memory.
The variable values of the basic data types are stored in stack memory, while object type variables, including functions, arrays, and objects, store only the address of the reference object in stack memory, which is the address assigned by the object in heap memory. Therefore, the variable value of the object type can be found through this address.
The above is how JavaScript allocates memory for variables. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are 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.