JavaScript scopes fine solution

[[Scope]] is a JavaScript function has each have an object, the object we can access some properties, but some do not, these properties are for the JavaScript engine access, [[scope]] is one of them. [[Scope]] refers to the scope Oh, we have to say, which was stored in the context of the implementation of the set.

  Execution of context: when the function is, will have to create an internal object called the context of the implementation period. A context implementation period is defined when a function execution was the environment, each execution when the function corresponding execution context is unique so repeatedly calling a function multiple times to create multiple execution contexts period, when the function is finished, it generates the context of the implementation period will be destroyed.

 

Scope chain: [[scope]] stored in the context of execution have obtained a collection of objects, the set of synthetic chain links, we call this chain link is called the scope chain.

such as:

function a() {
}

When a function is defined, a is the scope chain a [[scope]] -> 0:. AGO {}

When a function is executed, a [[scope]] -> 0:. AAO {}

                                                              1:GO{}

Is performed after a finished, aAO {} it will be destroyed. a scope chain back to the state defined. When a re-executed, will then create a new aAO {} object, and then on the top of a scope chain. (About this AO and GO, check out the pre-compiled JavaScript's )

var temp = 123;
function a() {
    var temp = 345;
  console.log(temp); }
a();

In the above procedure, the results of running 345; for a time of execution of a [[scope]] -> 0: aAO {temp = 345}.

                                                                                                                    1:GO{temp = 123}

The program is now scope topmost 0: aAO find the need to print variable temp, found in aAO in, use this temp, so the result is that print 345, when the function of a "var temp = 345; "to remove the case then aAO a temp does not exist in this variable, this time will be the next to find a scope chain. , In this case, it is to find the GO, find in the GO, GO to print this in temp. If you searched does not exist, then it being given that the use of an undeclared variable.

 

function a() {
   function b() {
        
   }
  b(); }

In this example, a is defined to be created in a context of execution, a [[scope]] -> 0:. AAO {}

                                                                                                           1:GO{ }

And b is defined in a function, the period b is executed at run-time context should be such that b [[scope]] -> 0:. BAO {}

                                                                                                                                       1:a.[[scope]]

That is b [[scope]] -> 0: bAO {}

                                   1: AAO {}

                                   2:GO{ }

 

Guess you like

Origin www.cnblogs.com/xiaonongfu/p/11002863.html