Several examples to understand stack memory processing for different data types

Time:2021-1-21

Please correct any mistakes

The running environment of JS code

  • Browser kernel (engine)
  • node
  • WebView (hybrid, embedded in mobile app, running in APP)

Here are a few examples to understand the stack memory processing of different data types

How to run JS (example 1)

var a = 12;
var b = a;
b = 13;
console.log(a);

The reason why the browser can run JS code is that the browser allocates a block of memory in the computer memory for code execution, which is calledStack memory, also known as stack, or ecstack (execution context stack)Execution environment stack

In order to distinguish which region (global or function, etc.) code is executed, aExecution context(EC : Execution Context)。 The so-called execution context is actually an abstract concept, which is the division of code execution area.

In the global environment, EC (g): execution context (golbal) is generatedGlobal execution contextWhere VO (g)Global variable object(variable object) stores the variables declared by the global execution context, and then enters the stack memory for execution.

To declare a variable

And then we go ahead, firstDeclare variablesThere are three steps to declaring variables

Var [variable] = [value]

  1. Create the value first (to the right of the execution equal sign)
    • The basic data type is stored directly in theStack memoryamong
    • The value of reference type is to open up a separate memory space(Heap memoryHeap) to store information
  2. Declare variable declare
    • Stored in the variable object of the current context (VO / AO)
  3. Define (assign) variable: Associate variable with value, that is, the so-called assignment operation, also known as defined or pointer pointing
    • thereforeVar n; // the default value is undefined

thereforevar a = 12The steps are

  1. Open up space in memory to store12value
  2. Declare variablesa
  3. take12Assign toa

andvar b = aThe treatment is

Because a on the right side is not a value, there is no need to take the first step. There is no need to open up space in the stack. You can directly declare in the second step, and then perform the third step to associate with12Value (pointer)

b=13The processing steps are as follows

  1. Open up space in memory to store13value
  2. becausebIt’s already in the variable object of the current context, so you don’t need to declare it in the second step
  3. take13Assign tob

result:

Overall execution logic:

How to run JS (example 2)

var a = {n: 12};
var b = a;
b['n'] = 13;
console.log(a.n);

When the value created in the first step is a reference type value, the value cannot be directly stored in the stack (there is not such a large space). When you create a reference type value, you do the following

  • Allocate a separate memory in computer memory (heap memory heap)
  • This heap memory has a hexadecimal address to look for
  • Store the key value pairs in the object into the heap memory
  • Put the heap memory address into the stack for variable call

That’s the first step,Create valueThe process of

The second step is to make a statement. In the third part, the assignment will be madeAddress in hexadecimalAssign to variable

var b = a;BecauseaIs a variable, so you don’t need to create a value and then declare itbFinally, assign a value to the stackaThe address pointed to is also assigned tob, letbIt also points to thatHexadecimal address

b['n'] = 13Operation principle:

b['n'] = 13Member access that belongs to the object

  • bFirst, based on the address0x000000Heap memory found
  • Make the members in heap memory asnChange the value of to13
  • console.log(a.n)Also belongs to member access, so output13

The order is as follows

Summary: what is the difference between basic data type and reference data type?
The value of the basic type is directly stored in the stack memory, and operates directly according to the value. The reference data type value is to open up a separate heap memory storage information, and the heap memory address is stored in the stack, and the operation is all reference address

How to run JS (example 3)

var a = {n: 12};
var b = a;
b = {n: 13};
console.log(a.n);

When it comes tob = {n: 13};Time

New heap memory{n: 13}And point the address to B

Go global object global object

Unlike VO, VO (g) is a global variable object that stores the values of variables declared by the current context

GO global object Global objects, which is the default form of loading page. In the browser, when loading a page, in theGlobal ContextBy default, awindowOf whichsetTimeout,setIntervalAnd other properties and methods for JS to call


Pay attention to distinguish VO and go

There is a special object in JavaScript calledGlobal objectIt and all its properties can be accessed anywhere in the program, that is, global variables
In browser JavaScript, thewindowIs a global object, not a node. The global object in JS isglobal, all global variables exceptglobalAll of them (except themselves)globalObject.
In node. JS, we can access it directlyglobalInstead of including it in the application.
globalprocess__filename__dirname

How to run JS (example 4)

var a = {n: 1};
var b = a;
a.x = a = {n: 2};
console.log(a.x);
console.log(b);
var a = {n: 1};
var b = a;

The first two lines of code are executed as follows

To understanda.x = a = {n: 2};

Let’s talk about operator priority briefly

var a=12,b=13;

amount to

var a=12
var b=13
var a=b=13

amount to

b=13
Var a = B // or var a = 13

Normal calculations are processed from right to left (of course, the first step is to create values)

But no matter whata.x=b=13stillb=a.x=13
We have to calculate firsta.xBecause of the high priority (the priority of member access is 19, second only to()Operations,Operator priority

a.x = a = {n: 2};The operation steps are as follows

  1. Open up memory, assuming the address is 0x000001
  2. Put the address on the stack
  3. a. X = address
  4. A = address

So nowaPoint to 0x000001,bPoint to 0x000000, i.e
aby{n:2}bby{n:1,x:{n:2}}

result:

A variable can only be associated with a stack value, but a stack value can be associated with multiple variables