Understanding closures in JavaScript

Source: Internet
Author: User

Closures are defined initially from a number of learning materials, such as closures that mean closed bracketing, used to simulate access to a private variable, access to member variables through the return of member functions//I might say it's just as embarrassing as C #.

Let's take a look at some examples.

    varFunc =function(N,C) {//This function creates an object            varName =n;//member Variables            varClassnum =C; return{//JSON notation that returns an object that accesses variables outside of this object_getname:function(){                    returnname; }, _setname:function(v) {name=v; }, _getclassnum:function(){                    returnClassnum; }, _setclassnum:function(v) {Classnum=v;                }                            }; }                varLiuzhe = func ("Liuzhe", 452);//Creating Objects                //alert (Liuzhe._getclassnum ());        //alert (Liuzhe._getname ());        //liuzhe._setname ("Light constant");        //alert (Liuzhe._getname ());        //alert (Liuzhe._getname ());

Here you can see the closure of a scenario: The returned object can access the function's name and Classnum, and can only be accessed through this return function, which encloses the name and Classnum variables, similar to the properties in C #, ensuring data security. And since the return object references the name and the classnum variable in its outer function, the func ("Liuzhe", 452) is executed, and then the external function is not GC, which keeps the two variables. So that the values of the two variables are kept in memory.

And look at the next piece of code.

<! DOCTYPE html>varOuter =function () {            varVariantouter = 1; varInner =function() {alert (variantouter); } variantouter=5; returninner; }        varTest =outer ();        Test ();    Test (); </script>

After executing the pop-up window is 5, not 1, because inner is a closure of outer, this closure refers to the outer variantouter, so when the outer () is executed, there is a outer function in memory, And its variantouter is 5, that is, the next number of changes, so inner find this variantouter is 5. So what if something like an onclick event is going to handle variables that are interpreted to inner? The answer is to create a closure with a function that executes immediately, and to execute the variantouter that is interpreted to inner as a function parameter. The code is as follows:

             var inner = (functionreturnfunction() {                              alert (arg);            };}) (variantouter);

Write a function that is automatically executed immediately, Variantouter, so that the immediately executed function creates a closure that saves the variantouter that the browser interprets at this point, rather than when looking for ariantouter, to find the last value in memory.

Understanding may be wrong, and the theoretical explanation is also very jerky, perhaps I cite the example is also very boring, so no one will feel useful ... 囧

Understanding closures in JavaScript

Related Article

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.