Objective:
Ah ah ah, reading really very painful ah, or good want to do the project code is interesting, but I now lack is indeed the knowledge of the system, so no matter how painful the reading must adhere to insist Ah, this is where I now most need the progress of the place, refueling!
Because the end of the current period, the next Monday is tomorrow is the beginning of the exam, so review is the main thing, reading speed is relatively slow, a week about 184 pages (p110-p284), although the number of pages is less but there are two of the most important concepts in ES- prototype chain and Closures , prior to the knowledge of the two know it is not know why, so in those two chapters I also intentionally spend more time, to ensure that I can thoroughly understand this concept. Here are some of my own understanding, if there is not very correct place, I hope the great gods to teach, Cancao Amida ~
Oh, by the way, I read the JS book is "Javascript Advanced Programming (3rd Edition)", if there are looking at this book of small partners can together wow ~ Everybody exchanges, learn from each other ~
Body:
Now the most popular programming language inside there must be "object" concept, our first experiment class teacher called us how to find the object, so the object is visible to the importance of the program ape, so you have the object? :) So how do we know which object is right for the thousands of the world? Can't you get along with one? Fortunately, we find that many objects have similarities, so we abstract their similar parts and form a new concept that, when we need more than our needs and this concept, we will save a lot of effort and get the results we want better and faster. And this concept in JS is the reference type, Object (JS does not have the concept of class, if you learn C + + or Java, C # and other languages, it will be easy to correspond to get to the reference type in JS). Object is really a very powerful part of JS, but it is not the most interesting, what is the most interesting? Function.
In JS, function is also an object. And the extension of the concept, such as inheritance, prototype chain and so on can be opened again, if it is really hungry, then go to search the Great God blog or find a "JavaScript Advanced program Design" and I study together ~ So, we now will be targeted, closures! Open fire, boom Boom Boom Boom
Before we talk about the concept of closures, we have to know what this noun means, right?
So what is a closure? Function.
Since it's just a function, why do you have to take a name specifically? Where is it especially?
The ability to access variables in another function scope. (The scope here is understood as {})
For example, the last example will know, don't ask me who the example is
function Outerfunc (Arg) { returninnerfunc() { alert (arg); } }
The function named Outerfunc here returns a function called Innerfunc, which has only one sentence of "alert (ARG)" Inside, but using the skill to look at the method, can not find Arg, and then up, wow, in the outerfunc parameters that found it, really happy. So this innerfunc (function) accesses a variable (ARG) within the scope of the Outerfunc (another function) (in the {} of Outerfunc, but not in the Innerfunc {}), so this innerfunc is a closure. (Most of the time, closures are not named, only for better descriptions here.) )
OK, the concept of closures know, then how to form closure of this phenomenon? Obviously just do not have the meaning, want to reject the scope of the action to make the trick, don't be afraid, we immediately won.
"When a function is called, an execution Environment and the corresponding scope chain are created. ”
"Each execution environment has an object- variable object that represents a variable. (Variable object). ”
Of course, we must first explain the noun, if you do not know what this noun refers to, then how to play.
Scope : A nickname for a variable object .
Scope Chain : Is the scope and chain Bai. A chain of scopes like bacon. It's just a chain. So the scope chain stores only the address of the variable object , not the variable object itself.
execution Environment : The execution Environment points to its own unique scope chain , scope chain According to the current function scope, external function scope, external function scope .... Until the global scope.
Again to understand, we wrote a function and then call it to implement the function, that JS exactly what to do for this. (1) When you create a function, you create a scope chain that contains the global variable object, which is stored in the internal properties [[scope]]. (2) When a function is called, an execution environment is created for the function, and then the scope chain of the execution environment is built by copying the function's [[scope]] property. (3) Create an active object (here, the variable object) at the forefront of the execution environment scope chain. When the function accesses a variable, that is, in the execution environment of the INNERFUNC function to "alert (ARG);", the execution environment finds its own scope chain of small partners, then the scope chain says no problem, I'll help you ask my scope brothers, Then the scope chain is asked Innerfunc () scope, "Hello, do you have arg this variable?" "I'm sorry, I don't have it." "Never mind." Thank you. "You're welcome." (The teacher teaches us to be a good and polite child.) Then it asks Outerfunc () scope, "Do you have arg this variable?" "I have OH." "OK, happy, found ARG, successfully executed" alert (ARG); statement, or until the global scope is still not found, it can only error. Generally speaking, the function is finished, then destroy it. However, the closure is so destroyed, where is it worthy of its special name?
1 functionOuterfunc (ARG) {2 3 return functionInnerfunc () {4 alert (ARG);5 } 6 7 }8 9 varresult = Outerfunc (1);//returns the Innerfunc functionTenResult ();//Output 1
Or just the function, or the original formula, add material, look at the effect.
As we have just said, Outerfunc should have been destroyed after the execution of Line 9th (in fact), but why is the implementation of line 10th still successful? Smart you must know, the relationship between scopes and scope chains. Outerfunc the execution environment and scope chain of the Outerfunc are destroyed after line 9th is executed, but! The scope chain of the Innerfunc also references the Innerfunc scope and the Outerfunc scope. The so-called "reference count is not unique". So, although Outerfunc has been destroyed, Innerfunc still has access to the value of the ARG variable. So you see the closure is not very difficult, do not be afraid of it, of course, I said here, are simple primary concept, the use of closures like CSS floating in the use of the same, know the concept is very easy to do things, but really use handy is really very high Dan thing, so together refueling ~ something: Tomorrow exam English, Hope to test well, I ask not high, on the 90 on the line AH. (forced to force, don't bother me)
About the closure of the understanding (JS Learning Summary)