JavaScript closure entry: Take the "Development Department" and "Technical Bull" for example.

Source: Internet
Author: User
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?&gt=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)

 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.