Tuesday 15 January 2013

javascript - accessing mutable variable in an event closure -


I am trying to use the Javascript plugin to handle some major strokes in a similar style, so I thought of them as code as follows:

  var key = ['B', 'I', 'U']; {Var iKey = keys [i] for (Var i = 0; i & lt; 3; ++ i); Var iKeyUpper = iKey.toUpperCase (); Mousetrap.bind (['command +' + iKey, 'command +' + iKeyUpper, 'ctrl +' + ikey, 'ctrl +' + ikeyupper], (function (e) {console.log ("You clicked:" + I);})); }   

But, obviously, i is unstable, though, I'm not sure how to write a stop, where I am competing with event parameters in response. How to handle this situation?

How can I write a stop where I'm competing in response to event parameters

Use a closure around the entire loop body (as @dandwiz is displayed), or use it around the handler:

  Â |? | Mousetrap.bind (['command +' + iKey, 'command +' + iKeyUpper, 'ctrl +' + ikey, 'ctrl +' + ikeyupper], (function (_i) {// Definitely you use the name (I.e.);}}}) (i) // and pass in the pass-through-reserve values;); console.log ("You clicked:    

No comments:

Post a Comment