Understanding scope chain

Scope
Scope, that is, the area or collection where variables and functions take effect
In other words, scope determines the code Visibility of variables and other resources in blocks

Divide the scope into:
Global scope
Function scope
Block-level scope domain

Global scope:
Any variable not declared in a function or in curly brackets is under the global scope. Variables declared under the global scope can be Access anywhere in the program

Function scope:
Function scope is also called local scope. If a variable is declared inside a function, it is under a function scope. face. These variables can only be accessed within the function and cannot be accessed outside the function

Block-level scope:
ES6 introduced the let and const keywords, which are different from the var keyword. Variables declared using let and const in curly brackets exist in In block-level scope. These variables cannot be accessed outside curly braces

Scope chain:
When using a variable in Javascript, the Javascript engine will first try to find the variable in the current scope
variable, if not found, go to its upper scope to search, and so on until the variable is found or it has reached the global scope
If it is in the global scope If the variable is still not found in the domain, it will implicitly declare the variable in the global scope or directly report an error

Guess you like

Origin blog.csdn.net/m0_45865109/article/details/127175240