Experience this series of video tutorials

Click to watch: Bilibili

function

The execution environment

  • The execution environment (execution context) isJSA very important concept.
  • Each execution environment has a virtual object associated with it (execution context object)
  • All variables and functions defined in the execution environment are stored in this execution context object for the parser to use when processing data.

pretreatment

  • The process of placing variables and functions on an execution context object as properties is calledpretreatment
  • Preprocessing occurs before the code is about to execute
  • althoughJSIt’s a language that explains execution, but before you explain execution, there’s apretreatmentThe process of
  • Prepare for code execution by detecting errors in the code ahead of time
  • It’s kind of a full sweep process
  • This is also the reason for the variable declaration promotion and function promotion mentioned earlier

Global execution context object

  • Formed in theJSBefore the code is to be executed
  • Is the top-level execution context
  • In the browserwindowobject
  • All global variables and functions arewindowObject properties and methods
  • Global variables and functions persist until the page closes
  • Destroyed as the page closes

Global preprocessing

  1. Will be sent to you byvarThe declared global variable is added towindowProperty of theundefined
  2. Will use thefunctionThe global function declared by the keyword is added aswindowThe value is the function body

Function execution context object

  • You have to call the function to create it
  • Formed before the code inside a function is about to execute
  • Each function has its own function execution context object
  • Local variables and local functions are destroyed at the end of the function call
  • Destroyed as the function call ends

Preprocessing of functions

  1. Will be sent to you byvarThe declared local variable is added as a property of the function execution context object with a value ofundefined
  2. Assign the argument to the parameter and add it as a function execution context object
  3. Will use thefunctionThe local function declared by the keyword is added as a method of the function execution context object with a value of the function body

scope

Scope refers to the scope within which a variable exists. In the ES5 specification, JS has only two scopes: one is a global scope, variables exist throughout the program, can be read everywhere; The other is function scope, where variables exist only inside a function. ES6 has added block-level scopes, which are not involved at the moment.

  • Variables that are not declared inside any function are global variables that can be retrieved and changed inside any function
  • Function passes throughvarDeclared variables are not available globally
  • Once the code is written, the scope is defined
  • Internal non of functionvarThe variable declared by the keyword is a global variable
  • There is no block scope
  • If there are nested functions, the scope chain is formed from the inside out