[Scope] lexical scope (static scope, such as: js), dynamic scope (such as: .bash script)

Scope

Scope refers to the region defined in the source program variables.

Scope specifies how to find variable, that is, to determine the current access code execution variables.

JavaScript uses lexical scoping (lexical scoping), which is static scope.

Static scope and dynamic scope

Since JavaScript uses lexical scoping, scope of a function when the function is defined on the decision.

With the lexical scope is relatively dynamic scope, scope of a function when the function is invoked before deciding.

Let's look at the difference between a serious example can understand:

var value = 1;

function foo() {
    console.log(value); } function bar() { var value = 2; foo(); } bar(); // 结果是 ???

JavaScript is assumed that static scoping, let's look at the implementation process:

Foo function performed, starting with the internal function foo find whether there is a local variable value, and if not, depending on the position of the writing, the top layer to find the code value is equal to 1, the result would be a print.

JavaScript is assumed that dynamic scoping, let's look at the implementation process:

Perform the function foo, foo still find from inside the function if there is a local variable value. If not, call the function from the scope, which is the internal bar function to find the value variables, so the results will print 2.

We've already said, JavaScript is used in static scope, so the results of this example is 1.

Dynamic scope

You might wonder what language is dynamic scope?

bash is the dynamic scope, do not believe it, save the following script to, for example scope.bash, then enter the appropriate directory, use the command line  bash ./scope.bash, look at the value of print is.

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

This file can also be  Github blog warehouse found in.

Questions

Finally, let's look at an example of a "JavaScript The Definitive Guide" in:

var scope = "global scope";
function checkscope(){ var scope = "local scope"; function f(){ return scope; } return f(); } checkscope();
var scope = "global scope";
function checkscope(){ var scope = "local scope"; function f(){ return scope; } return f; } checkscope()();

Guess their two code execution result is how much?

Here to tell you the results, two pieces of code will print: local scope.

The reason is simple, because JavaScript is used in scope lexical scope, a function of location-based function to create.

The reference to "JavaScript The Definitive Guide," the answer is:

JavaScript function used to perform the scope chain, the scope chain is created when the function definition. Nested function f () is defined in the scope chain, wherein the variable is a local variable scope must, wherever and whenever they perform the function f (), this binding is still valid when executed f ().

But here really want you to think about is:

Although the results of two pieces of code execution, but two pieces of code exactly what difference will it make?

To answer this question, we must involve a lot of content, lexical scoping is just a small part, let us look forward to the next article ---- "JavaScript execution context stack of depth."

Original: https://www.cnblogs.com/guaidianqiao/p/7762070.html

Guess you like

Origin www.cnblogs.com/liujinyu/p/11266402.html