JSFiddle - React, Tailwind, and code Playground
HTML
<p>
You might expect that the output of the callbacks would be ,0,1,2,3,4,5,6,7,8,9. But the results are actually:
</p>
<div id="results"></div>
JavaScript
(function(){
var results = false;
var callbackList = [];
// just a simple method to print the given value
function printIt(it){
if(!results) results = document.getElementById('results');
results.appendChild(document.createTextNode(','+it));
}
// store up some callbacks to illustrate the problem
for(var i=0; i < 10; i++){
callbackList.push(function(){
// print the current value of the i variable
printIt(i);
});
}
// loop through and execute callbacks
for(var callback in callbackList){
if(!callbackList.hasOwnProperty(callback)) continue;
callbackList[callback].call(this);
}
})();