JavaScript runtime and scope chain

Running environment : It is the environment where the JavaScript code segment is executed (can be understood as the scope in C/C++) [personal understanding]

Variable object : First of all, it is an object, which stores all variables and functions of the current execution environment (functions in JavaScript can be nested, but C/C++ does not seem to be able to (the new version seems to be able to))

Correspondence : Each execution environment has an associated variable object

Scope chain : chain, linked list.

     Role: Guarantee ordered access to all variables and functions that the execution environment has access to (what the heck, test language?)

current variable object → variable object containing environment → variable object containing environment ... → variable object of global execution object

Note: There is no concept of block-level scoping in JavaScript

if (true) {
    var color = 'red';
}
console.log(color);

It's true in JavaScript, but it's not true in C/C++

The code below is problematic

function outFun () {
     var color1 = 'yellow' ;
    function innerFun () {
         var color2 = 'green' ;
    }
    console.log(color2); // incorrect 
}

Why not? Doesn't js have block-level scope? Yes, there is no block-level scope. But there is an execution environment (scope)

The execution environment of color2 here is valid in innerFun (to understand the scope of C/C++)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279007&siteId=291194637