Recent Posts

Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

JavaScript closure inside loop example

When we want to bind a variable within an anonymous function unchanging value outside of the function, you can implement the following javascript code: var funcs = []; function createfunc(i) { return function() { console.log("My value: " + i); }; } for (var i = 0; i < 3; i++) { funcs[i] = createfunc(i); } for (var j = 0; j < 3; j++) { funcs[j](); // and now let's run each one to see } Since...