scope

[[scope]]: Every JS function is an object. Some properties of the object can be accessed, such as test.name of the function test, and some properties cannot be accessed. These properties are only accessible to the JS engine, and [[scope]] is one of them. [[scope]] refers to the scope generated by the function. It is an implicit property that is periodically called by the system and stores a collection of run-time contexts

The scope chain

A collection of execution-time context objects stored in [[scope]]. This collection is linked in a chain called a scope chain.

Runtime context

When the function executes, an internal object is created that becomes the execution-time context AO. An executor context defines the context in which a function is executed. Each time a function is executed, the corresponding execution context is unique. Therefore, multiple calls to a function result in the creation of multiple execution contexts, which are destroyed when the function completes execution.

To find the variables

Start at the top of the scope and start at the top of the scope

Patients with a

function a(){ function b(){ var b =234; } var a = 123; b(); } var glob = 100; a(); The [[scope]] function contains a scope chain, which contains a collection of execution contextCopy the code

Example 2

function a(){
    function b(){
        function c(){

        }
        c();
    }
    b();
}
a();
Copy the code
A defined a.[[scope]]-->0: GO a doing a.[[scope]]-->0: aAO 1: GO B.[[scope]]-->0: aAO 1: [[scope]]-->0: bAO 1: aAO 2: GO c defined C.[[scope]]-->0: bAO C.[[scope]]-->0: cAO 1: bAO 2 : aAO 3 : GOCopy the code

After c destroys the execution context of C, it returns to the defined state, and regenerates into C, the 0 step is different, and the rest is the same, generating the execution context and putting it at the top of the scope

/ / case

function a(){ function b(){ var bb = 234; aa = 0; } var aa = 123; b(); console.log(aa); } var glob = 100;} var glob = 100; a();Copy the code