JS knowledge 2

1. Basic execution principles of basic data types

var a = 12
var b = a
b = 13

console.log(a) / / 13
Copy the code

The execution principle is as follows:

2. Refer to the underlying execution principle of data types

  • object
var a = {n: 12}
var b = a
b['n'] = 13

console.log(a.n) / / 13
Copy the code

The execution principle is as follows:

var a = {n: 1}
var b = a
a.x = a = {n: 2}

console.log(a.x) // undefined
console.log(b) // { n: 1, x: { n: 2 } }
Copy the code

The execution principle is as follows:

JS operator priority

  • function

Create a function:

  • Create a heap memory to store the code in a function as a string
  • The scope of the declared function is the context in which the function is currently created (EC(G)).

Function execution:

  • Generate a private context (EC(FN1)), AO(FN1) private variable object, push
    • Generate function scope chain

      (< current function private context, the context in which the function was created >)
      (fn1),>
    • Initialize this and arguments
    • Parameter assignment: Declares a parameter variable and assigns the passed argument to the parameter
    • Variable ascension
  • Code execution (take the heap code string stored in heap memory when creating a function and turn it into code, execute)
  • The stack release

Code execution, code execution: private context variables, first of all, whether their own “private variables”, if it is, is their own operation, not, give up the scope chain, private and see whether it is the superior context is not continued to find upwards, until find the global context EC (G). This lookup process is called the scope-chain lookup mechanism.

var x = [12.23]
function fn(y) {
    y[0] = 100
    y = [100]
    y[1] = 200
    console.log(y)
}
fn(x) / / [100, 200]
console.log(x) / / [23] 100,
Copy the code

The execution principle is as follows:

  • The private variable y is the address of the argument passed in: 0x000001
  • Y [0] = 100, the value of index 0 in 0x000001 is changed to 100
  • Y = [100], [100] is an array, open a new heap memory 0x000002, private variable y pointing to change
  • Y [1] = 200, then the value of index 1 in 0x000002 is changed to 200

3. Browser garbage collection mechanism

GC: The browser’s garbage collection mechanism (memory free mechanism)

  • The page loads, forming a global context, and the context is released only when the page is closed
  • Function execution forms a private context and is pushed into the stack. When the code in a function completes execution, in most cases, the resulting context is removed from the stack

“Heap free”

  • Tag reference (Google)
    • The browser looks at all heap memory at idle or for a specified period of time, marks any heap memory that is not occupied by anything, and frees it when the collector runs
  • Reference Counting (IE)
    • The heap is created and used once, then the browser counts +1, the unused count -1, and when the number recorded is 0, the memory is freed (this method can cause cyclic reference problems).