Scope
What is a scope?
Scopes are the accessibility of variables, functions, and objects when your code is running . in other words, the scope determines whether the variables in your code can be referenced by the outside world.
Why do I need scopes?
Minimum access principle. Scopes provide a level of security for your code, with different scopes interfering with each other. Facilitates tracking of bugs and avoids conflicting names of variables.
What are the scopes?
Global scope (window)
Local scope (function)
Block-level scope (ES6 let/const keyword)
The scope in JS refers to the lexical scope. A section of JS code execution takes 2 stages:
1. Compile stage--the compiler compiles your code (lexical), and at this time, your scope is determined.
2. Execution phase--js engine starts to execute your code
The scope of JS is often confused with the execution context. How do you differentiate it?
The scope is determined at compile time, and the execution context is created when the function is executed (the execution context will be addressed later, note that the global scope can be considered a special ' execution context ', and the window is first understood as a function bar)
JavaScript---Scopes