scope

A scope is the area of a program’s source code where variables are defined.

A scope specifies how to find a variable, that is, determine what access the currently executing code has to the variable.

ECMAScript6 previously had only global and function scopes.

JavaScript uses lexical scoping, also known as static scoping.

Dynamic scope

You might be wondering what languages are dynamically scoped?

Bash is a dynamic scope. If you don’t believe me, save the following script as scope.bash, then go to the corresponding directory, run bash./scope.bash on the command line, and see what the printed value is

value=1
function foo () {
    echo $value;
}
function bar () {
    local value=2;
    foo;
 }
barCopy the code

This file can also be found in demos/scope/.

More and more

More articles in the JavaScript In-depth series can be found at github.com/mqyqingfeng… To view