Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More Text Challenge” [click here for more details]

Function creation

The creation of a function creates a space in the Heap memory to store the function. Objects create key-value pairs that store objects in heap memory, while functions store three things in heap memory:

  • [[scope]]
  • Function string
  • Key/value pair

Such as:

Let a = [1, 2]; function fn(b) { b[0] = 3; b = [4]; b[1] = 5; console.log(b); } fn(a); console.log(a);Copy the code

The object contains functions, but there are some differences in how functions are created and how other objects are created:

  1. Allocate a block of space (hexadecimal address) in the Heap memory.
  2. Store related content:
  • Function objects store three parts
    1. When creating a function, declare the scope [[scope]]. The scope is the context in which the function is created.
    2. Store the code in the function body as a string.
    3. Store some key-value pairs of functions as normal objects
  • The remaining objects store key-value pairs
  1. Assigns the created memory address to the corresponding variable or function, variable subsequent operations.

Function execution

1. Create a private context

Once executed, the function creates a new private context “push”. Each execution of the function forms a new private context, with no necessary connection to the context generated by the previous execution. The code in the function is executed in the private context. The stack execution creates a private variable Object AO(Active Object), which is distinguished from VO. AO is a branch of VO. Variables created in private contexts, such as parameters, variable promotions, and variables defined in functions, are stored in the AO.

2. Complete the initialization

After the function is pushed, some initialization operations are performed before execution begins:

  • Initialize the scope chain << its own private context, scope >>.
  • Initialize this (the arrow function does not have this step).
  • Initialize arguments (arrow functions do not have this step).
  • Parameter assignment.
  • Variable promotion.

3. Code execution

Executes the code strings that exist in heap memory from top to bottom.

4. Release or retain the stack

Normally, after code execution is complete, the private context is reclaimed from the stack. However, in special cases, if something in the current private context is occupied by something other than the execution context, the current private context will not be released from the stack, that is, an undestroyed context, closure, is formed. In addition to the above two cases, there is also a case where the context is not used, but it is used again, so that it cannot be released until it is used up, so that it forms a temporary not released.

Each execution of a function regenerates a new private context, meaning that if a function is executed 100 times, the steps above are executed 100 times, and 100 execution contexts are formed. Normally, the function is released when it finishes executing, but if it cannot be released (closures), it is kept in memory forever, and a stack burst occurs when it reaches the limit of stack memory.

Again, use this code as an example:

Let a = [1, 2]; function fn(b) { b[0] = 3; b = [4]; b[1] = 5; console.log(b); } fn(a); console.log(a);Copy the code

reference

  • Blog.csdn.net/aningde/art…
  • Blog.csdn.net/weixin_4206…
  • www.cnblogs.com/mttwind/p/1…
  • www.cnblogs.com/Jm-jing/p/7…
  • zhuanlan.zhihu.com/p/67132388