Javascript[1]-closures

Source: Internet
Author: User

    • Closures: in a programming language, a so-called closure is a paragraph in which the grammatical field is located in a particular region and has the ability to continuously reference (read and Write) a non-persistent variable value on the execution domain outside its own range within that Interval. The non-persistent variables of these external execution domains magically preserve their values at the time the closure was originally defined (or created). That is, the way in which another Function's internal variable can be accessed through one function is called a closure (creating an intrinsic function and returning it).
    • Variable action chain Field:
<script>    function  outfun () {       var num = 1;        function infun () {          console.log (num);       }    } </script>
    • Closures:
<script>    function  outfun () {       var num = 1;        function infun () {          console.log (num);       }        return infun;    }     var demo = Outfun ();    Demo (); </script>
    • Classic Interview Questions:
<script>    function  outfun () {        var a = 0;         function infun () {            + +;            Alert (a)        }        return  infun;     var obj = outfun ();    Obj ();    obj ();     var obj1 = outfun ();    Obj1 ();    Obj1 (); </script>

1 2 1 2

Closure Benefits: No global variables are generated and property privatization is Implemented.

Disadvantage: the closure of the data will be resident memory, when not used to delete, otherwise it will cause memory overflow.

    • Closed Packet Parameters:
<script>    function  fun (x) {        returnfunction  (y) {            Console.log (x+y);        }    }     var obj = fun (2);    Obj (3); </script>
    • Event Parameters:
<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title></title>    <style>Div{position:Absolute; left:0px;width:200px;Height:200px;Background-color:Pink;        }    </style></Head><Body><ButtonID= "btn2">Left</Button><ButtonID= "btn1">Right</Button><DivID= "box"></Div><Script>    varBTN1=document.getElementById ("BTN1"); varbtn2=document.getElementById ("btn2"); varBox=document.getElementById ("Box"); functionmove (speed) {return function() {box.style.left=Box.offsetleft+ speed+ "px"; }} Btn1.onclick=Move (5); Btn2.onclick=Move (-5);</Script></Body></HTML>

Javascript[1]-closures

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.