According to Us director Quentin Tarantino, 80% of the world’s stories have already been shot. So we’re going to do the old story in a new way.

All JavaScript variables (including functions) are stored in memory for the entire processing process, so one variable is processed. First you have to allocate memory for variables. Like other languages, JavaScript memory allocation is based on the data type of a variable, which is determined by the type of the value assigned. The data types supported by JavaScript fall into two broad categories: basic data types and complex data types. The basic data types include number, string, Boolean, undefined and null. Complex types include object types. Arrays and functions are all object types in JavaScript. All data types except basic data types are object types. In JavaScript, primitive data type variables are allocated in stack memory, which holds the value of the variable and is accessed by value; Variables of object type allocate both stack memory and heap memory, where the stack memory holds the address. Heap memory holds the referenced values, and stack memory addresses the heap memory values. Access to this variable is by reference, that is, first read to the stack memory address, then find the heap memory by address to read the value there.

The main reason JavaScript allocates memory according to different data types of variables is that stack memory is smaller than heap memory and the stack memory size is fixed, whereas heap memory size can change dynamically. The values of the base data types are fixed in size, while the values of the object types are not, so it makes sense to store them in stack memory and heap memory separately.

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 = ['a'.'b']; //arr is an array variable
var student = new Studenta(1."199706010016"."maomin".23); // Student is an object variable
Copy the code

The Student variable in the above code defines a constructor whose definition code is stored in heap memory and whose address is stored in the Student variable. The constructor is used to create the 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 the address allocated in the heap to which the student variable is assigned.

The {} in the above code creates an empty object in the heap memory whose heap address is assigned to the variable obj. [‘a’,’b’] is an array object whose elements are ‘a’ and ‘b’ respectively. This object is also stored in the heap, and its corresponding address is assigned to the arR variable. With the exception of Student, OBj, ARr, and Student, which are object variables, num, BOL, and STR are all primitives and are stored in stack memory.

The values of variables of basic data types are stored in stack memory, whereas object type variables, including functions, arrays, and objects, are stored in stack memory only at the address of the reference object, which is the address of the object allocated in heap memory, so that the value of the variable of the object type can be found at that address


Welcome to pay attention to my public account “front-end history robbery Road”

Reply keyword e-books, you can get nearly 12 front-end popular e-books.

Reply keywords little Red Book 4th edition, you can get the latest “JavaScript advanced Programming” (4th edition) ebook.

You can also add me to wechat. I have wooed many IT bigwigs and created a technical exchange and article sharing group. Welcome to join.

  • Author: Vam’s Golden Bean Road

  • Main area: front-end development

  • My wechat account is Maomin9761

  • Wechat public number: the front end of the road through robbery