About closures, closures

Source: Internet
Author: User

About closures, closures

I have been studying closures these two days. Because there were not many contacts with closures, I found a lot of information on the Internet. If you are afraid of forgetting, log on to your blog and record it.

What is a closure? When I first learned about JS, the closure is the variable that calls internal functions externally. At that time, the author thought it was a very simple thing and did not go into details. However, as there were more code written, the closure was very useful. So far, anonymous function Self-calls are used.

First, let's look at my two cases, scope and execution sequence.

<script>     for (var i=0; i < 10; i++) {           function aa(){               alert(i);           }     };     aa();//10     for (var i=0; i < 10; i++) {           function aa(){               alert(i);           }            aa();//1...9     };     </script>

The first pop-up is 10. After the for loop is executed, the aa () function is called. At this time, I is equal to 10, so alert (I) this I references I = 10;

The second one we all know is that the pop-up 1... 9, because one side is executed cyclically, we should all understand this. The second function is equivalent to the following anonymous self-called function.

 for (var i = 0; i < 10; i++) {           (function(j){             alert(j)           })(i)    };

In fact, the above is already called a closure.

What is closure? The process of allowing external functions to access internal function variables is called closure.

Generally, external functions cannot access internal functions. To access the service, we can only access the service through the closure, that is, the internal function is returned.

See the following:

   var aa=99;      function f1(){          alert(aa);      }      alert(aa);//99          function f2(){         var bb=99;      }      alert(bb);//error;      function f3(){          cc=88;      }      f3();      alert(cc);//88
Defines the global variable aa is equal to 99, according to the scope of access to internal functions (ACCESS)-external-parent function rules (everyone is called chain scope ). All functions can access aa. (all our functions are actually closures under the window object, because window is the parent of all functions );

99 is displayed after f1 () is executed;

F2 () is an error after execution because the internal variable bb is called externally;

F3 () returns 88 because I didn't use the var declaration to execute f3 and change cc to a global variable, which can be accessed externally;

How can external functions access internal function variables? Define an internal function in the function and then return it;

      function f1(){           var aa=99;           var f2=function(){               return function(){                    return aa;             }();         };             return f2();      }      alert(f1());
In this way, the external function calls aa. In this case, the memory does not clear aa, because the closure can save some variable values.

Function f1 () {var aa = 99; aadd = function () {aa + = 1}; var f2 = function () {return function () {window. alert (aa) ;}() ;}; return f2; // return function body} var result = f1 (); // reference the F1 function. Note that f2 is referenced by a global variable at this time, causing the memory to keep the value; result (); // execute f1 99 aadd (); // execute the global variable function aadd; aa is always stored in the memory. Result (); // when f1 100 is executed

Closure is similar to this. Let's take a look at the online search examples.

  var name="The Window";       var object={             name:"kack",             packname:function(){              return function(){                      return  this.name;                 }             }       }       alert(object.packname()());      var name="The Window";       var object={             name:"kack",             packname:function(){                var that=this;              return function(){                      return  that.name;                 }             }       }       alert(object.packname()());

These two are also closures, but there is an extra this pointer. One is window and the other is object. Therefore, the answer is ths window. One is kack. At the end, I run the function and the memory is released.

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.