Closure , The official explanation for closures is : An expression that has many variables and the environment to which they are bound ( Usually a function ), So these variables are also part of the expression . Characteristics of closures :   1. As a reference to a function variable , When the function returns , It is active .   2. A closure is when a function returns , A stack that does not release resources .   

To put it simply ,Javascript Internal functions are allowed --- That is, the function definition and function expression are in the function body of another function . and , These inner functions have access to all the local variables declared in the outer function they are in 、 Parameters and other declared intrinsic functions . When one of these inner functions is called outside the outer function that contains them , It's a closure .

 Common pitfalls :
function createFunctions(){
   var result = [] ;
   for ( var i=0; i < 10; i++){
     result[i] = function (){
       return i;
     };
   }
   return result;
}
var funcs = createFunctions();
for ( var i=0; i < funcs.length; i++){
   console.log(funcs[i]());
}
 
I thought the output was 0~9, In fact, the world is 10 individual 10, I thought he was waiting for for The assignment will be done after the loop , Not every cycle
 example :
Loop binding elements :
Method 1 :
<div class="button">1</div>
<div class="button">2</div>
<div class="button">3</div>
<div class="button">4</div>
<script type="text/javascript">
var div = document.getElementsByTagName('div')
for( var i=0;i<div.length;i++) {
(function (i) {
console.log(div[i].innerHTML);
div[i].onclick=function () {
alert(div[i].innerHTML)
}
})(i);
}
</script>
This is a simple closure , It loops through the binding <div> Elements , Click which <div>, It will pop up <div> Content contained To be continued ...

js More articles on closure binding elements

  1. js Custom method binding element Events

    // Event binding encapsulation function addEvent(elem, type, handle){ if(elem.addEventListener){ elem.addEventListener(typ ...

  2. Js by Dom Element binding events

    For asynchronously loaded Dom Element binding events must be bound after loading is complete : $('body').load('LearnClickBinding.ashx');$('a').click(function () { ale ...

  3. Native JS Event binding methods and jQuery Bind event method bind、live、on、delegate The difference between

    One . Native JS Event binding method : 1. adopt HTML Property to bind event handling functions, such as : <a href="#" onclick="f()"> 2. adopt JavaS ...

  4. I was asked during the interview js Binding events for , I don't know how to answer . I came back to check , Take a note

    Event binding is a couple of ways Here is an example : <button id='btn'>click me</button> function Btn(){ alert('click'); } 1. straight ...

  5. Vue.js The implementation principle of bidirectional binding and template engine (##########################################)

    Vue.js The implementation principle of bidirectional binding analysis magical Object.defineProperty This method is amazing ..vue.js and avalon.js It is through it to achieve two-way binding .. and Object.obser ...

  6. js Closure understanding instance summary

    Js Closure What you need to know before closure   1.  Function scope (1).Js The special feature of the language is that the global variables can be read directly inside the function <script type="text/javascript"> ...

  7. js Closures and ie Memory leak principle

    Also discuss js Closures and ie Memory leak principle Sure , But use it carefully . Closures may be JS It's the most useful feature in . There is a good document about closure principle . One thing to remember , A closure retains a pointer to its closed scope , therefore , In giving ...

  8. js Closure instance summary

    This article is to help you understand deeply through examples js Closure , It's a very good article , Here we recommend to you , If you need help, you can refer to Js Closure What you need to know before closure 1. Function scope (1).Js The language is special in that it can be read directly inside the function ...

  9. About js Small problem of closure, big mistake

    Closure is JavaScript A key aspect of development : Anonymous functions can access variables in the parent scope . If the scope of the closure holds a HTML Elements , The element cannot be destroyed .( The following code comes from elevation ) I just saw a note about closure ...

Random recommendation

  1. Git Version control management learning notes 4- File management and indexing

        It can be thought that Git when , We will meet 3 Space : working directory . Indexes . Version Library . What we care about , It's new . When modifying, etc , What changes have taken place among the three .     Generally speaking , It's editing in the working directory , Accumulate changes in the index , ...

  2. dynamic sql Statement output parameters

    ) declare @cou int ) ' set @sql='select @count=count(*) from emp where id=@id' exec sp_executesql @s ...

  3. How to decode Excel The edit password of the document

    about Excel We can not only set the password to open the document , You can also set several passwords for a few days , Like editing passwords . Edit password is also called write protect password , It's a kind of password that can restrict the editing permission . If we find ourselves forgetting in our daily work excel Edit the password , That's what we need ...

  4. APP Design dimension specification ,APP Interface design novice tutorial 【 Official edition 】( turn )

    Comes at a time 25 On the first anniversary of the school , At the same time, the stationmaster and APP Design colleagues in the group (APP Interface design UI Design communication group ,APP Interface design ⑥ Group  APPUI Design ③ Group 58946771 APP Design resources ⑤ Group 386032923 Welcome to join the exchange ...

  5. node-gyp rebuild Get stuck ?

    lately npm install I often meet in node-gyp rebuild It's been stuck there for a long time ( More than ten minutes ), So I studied the output error log to solve this problem , Share it here . First , Please check node-g ...

  6. SSH Simple learning of framework —Structs Study

    One :struts part 1. open Myeclipse, Create a web project, Project name: SSHDemo. 2. stay web Of lib Paste below struts2-blank.war After decompressing WEB-INF\lib Next ...

  7. underscore.js,jquery.js Source code reading

    (function() { // Baseline setup // -------------- // Establish the root object, `window` in the brow ...

  8. Android Mobile client test point ( whole )

    I saw a relatively complete mobile client test on the Internet :

  9. springboot The log is missing at startup Mapping Log etc.

    Discovery start springboot The diary is incomplete Missing startup log such as : Normal startup log terms of settlement : Replace the lower version of spring boot Parent dependency . <parent> <groupId>org.s ...

  10. rediret Add / With or without / The difference between