Experience this series of video tutorials
Click to watch: Bilibili
function
The execution environment
- The execution environment (execution context) is
JS
A 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 called
pretreatment
- Preprocessing occurs before the code is about to execute
- although
JS
It’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 the
JS
Before the code is to be executed - Is the top-level execution context
- In the browser
window
object - All global variables and functions are
window
Object properties and methods - Global variables and functions persist until the page closes
- Destroyed as the page closes
Global preprocessing
- Will be sent to you by
var
The declared global variable is added towindow
Property of theundefined
- Will use the
function
The global function declared by the keyword is added aswindow
The 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
- Will be sent to you by
var
The declared local variable is added as a property of the function execution context object with a value ofundefined
- Assign the argument to the parameter and add it as a function execution context object
- Will use the
function
The 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 through
var
Declared variables are not available globally - Once the code is written, the scope is defined
- Internal non of function
var
The 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