This is a creation in Article, where the information may have evolved or changed. What is a closure package? What are the pros and cons of closures? Closures (closure) is one of the most difficult and unique features of JavaScript. Many advanced applications rely on closures to implement them. 1, variable scope to understand the closure, the first to understand the special variable scope of JavaScript. The scope of a variable is nothing more than two kinds: global variables and local variables. The special thing about JavaScript is that the global variables can be read directly inside the function, but local variables inside the function cannot be read outside the function. NOTE: When declaring variables inside a function, be sure to use the var command. If not, you're actually declaring a global variable! 2. How to read the local variables inside the function from the outside? For a variety of reasons, we sometimes need to get local variables inside the function. However, as already mentioned above, under normal circumstances, this is impossible! This can only be achieved through a workaround. That is, inside the function, define a function. "' Javascriptfunction F1 () {var n=999; function F2 () {alert (n);//999}}" in the above code, the functions F2 is included inside the function F1, then all local variables inside the F1, are visible to the F2. But the opposite is not possible, F2 internal variables, the F1 is not visible. This is the JavaScript-specific "chain-scoped" structure (chain scope), where child objects look up the variables of all the parent objects one level at a level. Therefore, all the variables of the parent object are visible to the child object, and vice versa. Since F2 can read the local variables in the F1, we can not read its internal variables outside the F1 as long as the F2 is the return value! 3, closure of the concept of the above code in the F2 function, is closed packet. The closure definitions of various professional literature are very abstract, and my understanding is that closures are functions that can read other functions ' internal variables. Because in JavaScript, only a child function inside a function can read a local variable, a closure can be simply understood as "a function defined inside a function". So, in essence, closures are bridges that connect functions inside and outside of functions. 4, closure of the use of closures can be used in many places. Its maximum usefulness is two, one of the previously mentioned variables can be read inside the function, the other is to keep the values of these variables are always in memory, will not be automatically cleared after the F1 call. Why is that? The reason is that F1 is the parent function of F2, and F2 is assigned to a global variable, which causes F2 to always be in memory, and F2 's presence depends on F1, so F1 is always in memory and will not be garbage collected after the call ends (Garbage coLlection) Recycling. Another notable part of this code is the line "nadd=function () {n+=1}", which first did not use the var keyword in front of Nadd, so Nadd is a global variable, not a local variable. Second, the value of Nadd is an anonymous function (anonymous functions), and the anonymous function itself is a closure, so nadd is equivalent to a setter that can manipulate local variables inside the function outside of the function. 5, the use of closure of the attention Point (1) because the closure will make the variables in the function is stored in memory, memory consumption is very large, so can not abuse closures, otherwise it will cause the performance of the Web page, in IE may lead to memory leaks. The workaround is to remove all unused local variables before exiting the function. (2) The closure will change the value of the inner variable of the parent function outside the parent function. So, if you use the parent function as an object, and the closure as its public method, and the internal variable as its private property (private value), be careful not to arbitrarily change the value of the inner variable of the parent function. 252 reads
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service