- 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>
<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>
<!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