Shadowing variables is tricky. The simple way JavaScript lets you write code often hides something a bit more complex. It pays to be more explicit about whatโs going on: passed by value function mechanism == implicit variable declarations (parameters), combined with variable shadowing.
Add this to the closure concept involved in the first code and you got a solid list for head scratching.
Without IIFE it should make more sense:
for(var counter = 1; counter < 10; counter++) {
message(counter);
}
// it doesn't work in IE < 9
//function message(ratio) {
// setTimeout( hello, 500 * ratio, ratio);
//}
// works everywhere
function message(ratio) {
setTimeout(hello.bind(null, ratio), 500 * ratio);
}
function hello(increment) {
console.log("Hello " + increment);
}