Although it's just a short time for cainiao to take study notes, I still hope that experts can help you with your advice when they see the shortcomings ~ I. Code and execution process/*** http://blog.csdn.net/ruantao1989?>=july6analytic=simplest closed package #/functiona () {... SyntaxHighlighter. al
Although it's just a short time for cainiao to take study notes, I still hope that experts can help you with your advice when they see the shortcomings ~
I. Code and execution process
/*** Http://blog.csdn.net/ruantao1989 * ==> July6 analyzes the simplest closure */function a () {// ==> from the perspective of variable I and object B: they are local variables in. if no closure exists, var I = 0 will be destroyed after a returns; function B () {alert (++ I ); // ==> B can access variable I in a (in scope)} return B; // ==> 1. return Value of function a, reference internal function} var c = a (); // => 2. at this point, variable c is the return value of function a: function B () // => so far, this simple closure is constructed: // 1. variable c is a reference to object B or function B (see figure); // 2. function a should be destroyed after return to c, but variable c should hold the reference of object B in a, so a cannot be destroyed. // 3. variable I belongs to a, the memory continues to survive, and executes its counting function // ==> 3. in each of the following calls to c (), I will automatically add (currently there is only one way to operate on the value of I in this Code) c ();
2. Let's talk about it in another popular way:
Cause:
1. Object a is the "Development Department" of the company. There are technical talents B and I who only remember numbers.
2. c is the last project in the company. The c project requires B from the "Development Department" to write code. The company has been fired if it wants to remove the "Development Department" from this project.
After:
Niu Ren B started to write code in Project c.
Result:
1. Therefore, the blessing of "Development Department" to "employee" B cannot be destroyed.
2. I, the number specialist of the "Development Department", was lucky to have B's fortune to continue to eat mixed meals in the "Development Department" and earnestly perform his work: Number
Iii. Examples of incorrect return
Function a () {var I = 10; function B () {alert (++ I) ;}// return B; // ==> no return B (), direct return I;} var c = a (); alert (c ); // ==> no matter how many times it is played, it is the initial value of I (each time I is a new variable)