1. Create a function
  • Create a heap of memory
  • Store the code in the function body as a string
  • Assigns the heap memory address value to the function/variable name
  • Where is the function created, and who is the parent scope that it needs to look up when it executes
  1. Function performs
  • Create a new private scope, execution context, and private stack memory (execute one at a time without affecting each other)
  • Parameter assignment & variable promotion & lexical parsing
  • Code execution (take the code string out of the heap memory and execute it line by line)
  • If a variable is a private variable, check whether it is a private variable (parameters and variables declared in private scopes are private variables). If it is a private variable, check whether it is a private variable. Until global scope is found => scope chain lookup mechanism
  • Private variables are not necessarily related to external variables and can be understood as being protected by private stack memory, which is essentially the closure protection mechanism
  1. About the stack memory free problem

Function execution will form stack memory (allocate a piece of space from memory), if the memory is not destroyed and released, it is easy to lead to stack memory overflow (memory overflow, computer stuck), stack memory release problem is one of the core knowledge to learn JS

  • Heap free problem: creating a reference type value creates heap memory.

If the heap currently created is not occupied by something else (the browser will look for references to each memory when it is free, and the unused memory will be reclaimed)

let obj = {
    name: 'jack'
} // Form a heap memory AF0
obj = null //obj no longer points to AF0, AF0 will be reclaimed
Copy the code
  • Stack memory release
    • The global scope created by opening the browser is the stack memory
    • The private scope created by manually executing functions is the stack memory
    • The block scope based on let/const in ES6 is also stack memory

Global stack memory: destroyed only when the page is closed

function fn(){}
fn() // Function execution forms stack memory, execution completes memory destruction
Copy the code

Private stack memory:

  1. Normally, as soon as the function completes, the resulting private stack memory is destroyed and freed (to exclude dead recursion).

  2. But once the stack memory of something (usually addresses) occupied by private things outside the scope of the current private stack memory cannot be released immediately destroyed (features: private scope of information such as private variables also retained) = > think of closure on the market: private stack memory function form cannot be released, such is the closure

funciton X(){
    return function(){}}let f = X() // if f occupies the stack memory of X execution (return the heap corresponding to the small function), the stack memory of X execution can not be freed
Copy the code
var i = 5
function fn(i) {
  return function (n) {
    console.log(n + (++i))
  }
}
var f = fn(1)
f(2) / / 4
fn(3) (4) / / 8
fn(5) (6) / / 12
f(7) / / 10
console.log(i) / / 5
Copy the code
var i = 20
function fn() {
  i -= 2
  return function (n) {
    console.log((++i) - n)
  }
}
var f = fn()
f(1) / / 18
f(2) / / 18
fn()(3) / / 16
fn()(4) / / 14
f(5) / / 14
console.log(i) / / 9
Copy the code
function fun(n, o) {
  console.log(o)  // The printing sequence is undefined, 0, 1, 1
  return {
    fun: function (m) {
      return fun(m, n)
    }
  }
}
var c = fun(0).fun(1)
c.fun(2)
c.fun(3)
Copy the code