Code classification (Location)

  • Global code
  • Function (local) code

Global execution Context (order)

  • Identify the window as a global execution context before executing global code
  • Global data is preprocessed
    • The global variable defined by var ==> undefined is added as the property of window
    • Function declared global function ==> assignment (fun), added as window method
    • This ==> assign (window)
  • Start executing global code
    console.log(a1) // undefined
    a2() // "a2()"
    var a1 = 3
    console.log(a1) / / 3
    function a2() {
      console.log('a2()')}Copy the code

Function execution context (order)

  • Before calling the function and preparing to execute the function body, create the corresponding function execution context object
  • Local data is preprocessed
    • Parameter ==> Assignment (argument) ==> added as an attribute of the execution context
    • Arguments ==> Assign (argument list) to add as an attribute for the execution context
    • The local variable defined by var ==> undefined is added as an attribute of the execution context
    • Global function ==> assignment (fun) declared by function, added as an attribute of the execution context
    • This ==> Assignment (the object calling the function)
  • Start executing the function body code
    function fn(a1) {
      console.log(a1) / / 2
      console.log(a2) // undefined
      a3() // "a3()"
      console.log(this) // [object Window]
      console.log(arguments) // Object { 0: 2, 1: 3 }
      var a2 = 3
      console.log(a2) / / 3
      function a3() {
        console.log('a3()')
      }
    }
    fn(2.3)
Copy the code