This blog is reproduced in: http://devbean.javaeye.com/blog/409566
The concept of closure looks very esoteric. The meaning of this word in discrete mathematics is indeed hard to understand. Here, we can first understand the closure as an anonymous function or an anonymous class.
1. What is a closure?
What is a closure? A formal explanation is: Closure refers to an expression (usually a function) that has many variables and is bound to these variables ), therefore, these variables are part of this expression.
I believe many people will not understand this definition, because his academic taste is too strong-maybe you like to analyze it in the literal Syntax: first, it is an expression, this expression is bound to many variables and the environment of these variables. However, this does not make any sense. It still does not tell us what a closure is.
Let's look at an example:
JS Code
Function add (a) {<br/> return function (B) {<br/> return a + B; <br/> }; <br/>}< br/> var func = add (10); <br/> alert (func (20 ));
I want to go through the previous descriptions of functions. This example should be clear and understandable. The function in Javascript is an object. It can do everything that the object can do. We first define a function add, which accepts a parameter. This function returns an anonymous function, this anonymous function also accepts a parameter and returns the sum of the parameters of this parameter and the external function. Therefore, when we use it, we assign the anonymous function returned by add to func, and then call func to return the sum of the two numbers.
When we create a function where a variable inside the function can be referenced outside the function, we call it a closure. Take a closer look: This is the definition of the closure.
Let's look at our code: First, it has an internal variable, namely the anonymous function. Second, this function returns the anonymous function, so that external variables can reference internally defined variables.
2. Functions of closures
What is the use of closures? Maybe I cannot see it yet. Let's take a look at this Code:
JS Code
Function Inc (a) {<br/> var I = 0; <br/> return function () {<br/> return I; <br/> }; <br/>}< br/> var num = Inc (); <br/> alert (Num ());
Originally, this variable I cannot be accessed outside the function, because it is defined by var. Once out of scope, this variable will be garbage collected. However, because we use closures, this variable can be accessed outside, so it is not recycled!
If you still do not understand the function of the closure, you should be familiar with the following code:
JS Code
Function person () {<br/> var ID; <br/> This. GETID = function () {<br/> return ID; <br/>}< br/> This. setid = function (newid) {<br/> id = newid; <br/>}< br/> var P = new person (); <br/> P. setid (1000); <br/> alert (P. GETID (); // 1000 <br/> alert (P. ID); // undefined
We define a class person, which has an ID attribute. Now this attribute acts like a private variable-it can only be accessed through the setter and getter functions. That's right. This is one of the purposes of the closure: the private variable of the manufacturing class!
The closure also has a role: maintain a variable in the memory and prevent the garbage collector from recycling the variable. The example here is no longer mentioned.
Here we just briefly talk about the concept of JavaScript closures, and there is no memory model involving closures, etc. This is a very important concept. Some members of the Java Community have always dreamed of closures. C # has also added the concept of closures in the latest version, but it is called a Lambda expression.