javascript - How do i pass a variable to a nested function? -


this question has answer here:

i have started use createjs , have come problem (which doesnt have t odo createjs):

for (a in ship.weapon) {      //code     button[a].addeventlistener("click", function() {          ship.weapon[a].amount = ship.weapon[a].amount.plus(1);     });     //code } 

the "a" variable ofcourse @ time button pressed lenght of ship.weapon array. how make "a" inside click function stay @ value of loop when made?

you can use closure freeze a value

for (a in ship.weapon) {      (function(index) {         button[index].addeventlistener("click", function() {              ship.weapon[index].amount = ship.weapon[index].amount.plus(1);         });     })(a); // calls function defined passing 'a' parameter } 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -