3 ms·
Here's a post on Stackoverflow that can explain it better than I can: http://stackoverflow.com/questions/21363295/understanding-javascript-closures-freeze-varia
by Meegul 10y ago
Here's a post on Stackoverflow that can explain it better than I can: http://stackoverflow.com/questions/21363295/understanding-javascript-closures-freeze-variable-passed-to-callback http://stackoverflow.com/questions/21363295/understanding-ja...
- z3t4 10y agoHere's how you should do it: for(var i=0; i<=3; i++) count(i); Or a real world example: for(var i=0; i<texture.length; i++) createPattern(i); function createPattern(i) { texture[i].onload = function() { pattern[i] = ctx.createPattern(texture[i], 'repeat'); } } I've made a blog post about closures: http://www.webtigerteam.com/johan/en/blog/closure_en.htm http://www.webtigerteam.com/johan/en/blog/closure_en.htm It would be cool to write like this (but you cant): pattern = texture.map(t => t.onload => ctx.createPattern(t, 'repeat'))