The scope chain

  1. When we look for a variable, the real order of lookup is in the scope chain

  2. The parent scope of a variable is static, not dynamic, which means that the parent scope of a variable depends on the level above where it is defined,

    It’s defined when the function is defined, not when the function is executed

let name = 'coderwxf'

function foo() {
  function baz () {
    console.log(name)
  }

  baz()
}

foo()
Copy the code

During execution, each function scope has a scope chain inside it, which represents the scope chain of the current function

When the baz function is executed, it prints the value of name,

  1. Look it up in your OWN AO
  2. If it does not exist in its own AO, look for it in its parent AO
  3. The parent AO does not exist, go to the parent AO
  4. .
  5. If GO still does not exist, an error will be reported

Scoping exercise

var n = 100

function foo() {
  // The function's scope chain is determined at compile time
  // static scope chain in JS
  console.log(n) / / = > 100
}

function baz() {
 var n = 200

 foo()
}

baz()
Copy the code
function foo() {
  var a = b = 100
  /* The above code will actually compile to var a = 100 b = 100 */
}

foo()

console.log(b) / / = > 100

console.log(a) // => error
Copy the code

Memory management

Regardless of the programming language, the code must be allocated memory during its execution,

The difference is that some programming languages require us to manage memory manually, and some programming languages help us manage memory automatically

  • Manual memory management: For example, C and C++, including early OC, require manual memory management (malloc and free functions).

  • Automatic memory management: Java, JavaScript, Python, Swift, Dart, etc., have automatic memory management to help us

Regardless of how memory is managed, memory management has the following lifecycle:

  • Step 1: Allocate and request the memory you need (request)
  • Step 2: Use allocated memory (to hold something, such as objects, etc.)
  • Step 3: Release it when it is not needed

So JS is automatic memory management, will automatically allocate and release memory for us

Memory management

JavaScript allocates memory for us when we define variables

  • JS memory allocation for basic data types is directly allocated in stack space at execution time
  • The JS allocation of memory for complex data types creates a space in the heap and returns the pointer to this space to the value variable reference

The garbage collection

Because the size of memory is limited, we need to free it when it is no longer needed to make more memory space

In languages that manage memory manually, we need to free up memory that is no longer needed by ourselves, such as the free function:

  • But this way of management is actually very inefficient, we need to pay attention to memory management, affecting the efficiency of our writing logical code
  • If we do not manage memory well, such as forgetting to release it, it will leak carelessly

So most modern programming languages have their own garbage collection mechanisms:

  • Garbage Collection is called Garbage Collection
  • Objects that are no longer in use are called garbage and need to be collected to free up more memory
  • Our language runtime environment will provide its own garbage collector in memory, such as Java runtime JVM, JavaScript runtime JS engine will automatically built-in memory collector
Common garbage collection algorithms

Reference counting

  • When an object has a reference to it, the object’s reference is +1

  • When a reference to an object is zero, it can be destroyed

One of the big drawbacks of this algorithm is that it can’t handle circular references, and if it does, it needs to be destroyed manually

Mark clear

This algorithm is to set a root object (root object){in JS, the corresponding root object is the GO object}. The garbage collector will start from this root periodically, find all the objects referenced from the root, and consider the objects that are not referenced as unavailable, and perform corresponding cleanup operations

This algorithm can solve the problem of circular reference very well

JS engine is widely used is the mark clearing algorithm, of course, similar to V8 engine in order to better optimization, it will also combine some other algorithms in the implementation details of the algorithm.