JavaScript closures in layman's

Source: Internet
Author: User

1, closures

1$ (document). Ready (function(){2 varspans=$ ("#divTest span");3  for(vari=0;i<spans.length;i++){4spans[i].onclick=function(){5 alert (i);6 }7 }8 }); 9$ (document). Ready (function(){Ten varspans2=$ ("#divTest2 span"); One varspans=$ ("#divTest span"); A  for(vari=0;i<spans2.length;i++){ -(function(num) { -spans2[i].onclick=function(){ the alert (num); - } - }) (i); -spans[i].onclick=function(){ + alert (i); - } + } A});

2, an intrinsic function that calls INNERFN in the OUTERFN scope is valid, and calling INNERFN outside of OUTFN is not valid.

1 function Outerfn () {2 document.write ("Outer function</br>"); 3 function Innerfn () {4 document.write ("Inner function</br>"); 5 }6innerfn (); 7 }8 outerfn ();

3,

1 varGlobalvar;2 functionOuterfn () {3document.write ("Outer function</br>");4 functionInnerfn () {5document.write ("Inner function</br>");6 }7Globalvar=INNERFN;8 }9 OUTERFN ();TenGlobalvar ();

How to return a value

 1  function   Outerfn () {  3  function   Innerfn () {  5   6  return   INNERFN;  7   8  var  fnref=OUTERFN ();  9  fnref (); 

As long as there is the ability to invoke intrinsic functions, JavaScript preserves the functions referenced by the chant. and the JavaScript runtime needs to keep track of all the variables referencing this intrinsic function, knowing that the last variable is obsolete and that the JavaScript garbage collector frees up the appropriate memory space. A closure is a function that has permission to access a variable of another function scope.
4, variable scope

1 functionOuterfn () {2document.write ("Outer function</br>");3 functionInnerfn () {4 varInnervar=0;5innervar++;6document.write ("Inner function\t");7document.write ("Innervar =" +innervar+ "</br>");8 }9 returnINNERFN;Ten } One varfnref=OUTERFN (); A fnref (); - fnref (); - varFnref2=OUTERFN (); the FnRef2 (); -FnRef2 ();
var globalvar=0;function outerfn () {document.write ("Outer function</br>"); function Innerfn () {globalvar++; document.write ("Inner function\t");d ocument.write ("Globalvar =" + Globalvar + "</br/>");} return INNERFN;} var fnref=outerfn (); Fnref (); Fnref (); var fnref2=outerfn (); FnRef2 (); FnRef2 ();

  

1 functionOuterfn () {2 varOutervar=0;3document.write ("Outer function<br/>");4 functionInnerfn () {5outervar++;6document.write ("Inner function\t");7document.write ("Outervar =" +outervar+ "<br/>");8 }9 returnINNERFN;Ten } One varfnref=OUTERFN (); A fnref (); - fnref (); -  the varFnref2=OUTERFN (); - FnRef2 (); -FnRef2 ();

1 functionOuterfn () {2 varOutervar=0;3document.write ("Outer function</br>");4 functioninnerFn1 () {5outervar++;6document.write ("Inner function 1 \ t");7document.write ("Outervar =" + outervar+ "</br>" );8 }9 functioninnerFn2 () {Tenoutervar+=2; Onedocument.write ("Inner function 2 \ t"); Adocument.write ("Outervar =" + outervar+ "</br>" ); - } - return{"FN1": InnerFn1, "fn2": innerFn2}; the } -  - varfnref=OUTERFN (); - fnref.fn1 (); + fnref.fn2 (); - fnref.fn1 (); + varFnref2=OUTERFN (); A fnref2.fn1 (); at fnref2.fn2 (); -Fnref2.fn1 ();

5, the interaction between closures

Subsequent calls to OUTERFN create new instances of these closures and create a new enclosing environment, essentially creating a new object, which is the object variable of the instance, and the closure is the instance method of the object.

JavaScript closures in layman's

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.