JavaScript get event with closure function? -


hi havr problem event , pass closure function want (this) varaible

i use

   console.log(e); 

its show undefined >> sloution best sloution :)

i try code

<script> for(var i=0;i<a.length;i++){ a[i].addeventlistener("keydown",(function(e){  return function(){ if (e.keycode == 13 && !e.shiftkey){  console.log(e);   // e undefined problem  console.log(i);   // value no problem here  //now iam use closure value //var = this;  }  };   })(e), false);  } </script> 

why not directly access e inside function

<script>     a[i].addeventlistener("keydown",(function(){         var index=i;         return function(e){             if (e.keycode == 13 && !e.shiftkey){                 console.log(this);                 console.log(e);                 console.log(index);             }         };     })(), false); 

working


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -