4. Scope and Closure
@ Wu Qiong Adam (SINA)
Note: read 1-3, http://blog.csdn.net/adwu73/article/details/7229563 before reading this section
Scope is a key concept in JS. Only by figuring out it can we figure out the closure (closure ). Javascript: the definitive guide explains scope clearly, while javascript: the good parts does not. Next we will use thisProgramScope and closure are described as follows:
For a JS program, there is a global scope object. The global variables we define are actually attributes of this object. In the main program body, you can use this to access the content of this object. In this case, there is only one global scope object in scope chain.
Run var counter = function () {…} according to the following principles (){...} Then, the objects in the system are shown as follows:
Scope Principle 1: When function f is defined, JS will save the currently valid scope chain in function object F for future reference.
Run var counter1 = new counter. According to the following principle, the system object is shown as follows:
Scope Principle 2: When a function is executed, a new scope object is created and the function content is completely analyzed, add the parameters and local variables to the attributes of the new scope object, and use the new scope object as the last section of the scope chain to update the current scope chain.
Scope chain is used to query attributes later. If this scope cannot be found, it will be queried in the parent scope until global scope object. In fact, it is also a delegation, so maybe the scope object is also connected with _ PROTO?
Then the counter content is executed, and the increment and getvalue functions are defined and then returned to counter1. According to principle 1, the system object is shown as follows:
After var counter2 = new counter is executed, the object becomes:
When we see the above object model, I don't think everyone will be surprised at the following section:CodeThe output is as follows:
Counter1.increment ();
Document. writeln ("counter1:" + counter1.getvalue ());
Document. writeln ("counter2:" + counter2.getvalue ());
Originally, it would not interfere with each other!
Finally, we provide the closure definition. The internal function g of a function f can access the local variables and parameters defined by F. This is called closure, which is the basis for encapsulation in Js!
For ease of narration, we have ignored some details about JS memory management in this section. The garbage collection and stack will be broken down in the next section! (Supplement: Later, you can think that all the memory of JS is allocated directly from it. There is no traditional stack concept, and objects do not need to be released by themselves, so there is a uniform responsibility for garbage collection, therefore, the next section will write something else)
Note: We recommend that you look at this blog, the explanation will be more detailed, there are more examples: http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html