What is scope

Scope refers to the scope (permission) demo of variables that the currently executing code can access

let a = 1
function fn1() {
    function fn2() {
        console.log(a)
    }
    fn2()
}
fn1()
Copy the code

As in the above code, the scope of fn2 is the environment inside fn2, and the scope of fn1 is the environment in fn1 code

What is a scope chain

Scope chain is refers to the current function is executed to access a variable, in the scope of the currently executing function without the variable, this will access the function outside of the scope, and if the currently executing function outside of the scope and does not contain the variable, will continue up access to external scope of external scope, until the global scope, So that’s the scope chain demo

let a = 1
function fn1() {
    function fn2() {
        console.log(a)
    }
    fn2()
}
fn1()
Copy the code

Fn2 first accesses the scope of the current function when printing variable a. If fn2 is not in scope, it will search up to fn1. If fn1 is not in scope, it will continue to find the global scope, and the variable a is declared in global scope

Lexical scope (static scope)/ dynamic scope

  • Lexical scope: The scope of a function is determined when the function is defined

demo

var value = 1;
function foo() {
    console.log(value);
}
function bar() {
    var value = 2;
    foo();
}
bar();
Copy the code

Since JavaScript uses static scope, the scope of foo is defined at the time of definition. The function foo is first accessed in its own scope during execution, and the value is not found. Therefore, the external scope of foo is specified, which is the global scope of the current code. Value in the global scope is 1, so the printed value is 1

  • Dynamic scope: The scope of a function is determined when the function call is executed

Demo: The bash language is dynamic scoping

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

In bash language, the scope of foo is determined when foo is called, and its external scope is bar, which automatically accesses value=2

Var, let, const declared block scope/function scope

Let, const: Let and const declare both function scope and block-scope demo

    function fn() {
        console.log(a)
        console.log(b)
        console.log(c)
        var a = 1
        let b = 2
        const c = 3
        console.log(a)
        console.log(b)
        console.log(c)
    }
Copy the code

Then print the result will be an error, but will be prompted to b Undefined, because the variables of the var statement in the current scope has the effect of the variables increase, the statement will be variable ascent to the top of the current scope, therefore in the scope of the current function to a variable can be accessed, and under different is in the upper part of a variable declaration value is Undefined, So var and const declarations are only in function scope. Let and const declarations are not promoted and can only be used below the declaration. This scope is block-level scope and can also be used below the declaration in a function

To consider

Think about the results of the following two problems

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