There is no "correct" way. There are different ways. In C#, you would fix it by making a variable scoped to the loop:
for (int i = 0; i < 10; i++)
{
int j = i;
f[i] = (new Action(delegate()
{
Console.Write(j + " ");
}));
}
In JavaScript, you might add a scope by making and calling an anonymous function:
for (var i = 0; i < 10; i++) {
(function(i) {
f[i] = function() {
document.write(i + ' ');
};
})(i);
}
Iteration variables in C# don't have loop scope. JavaScript doesn't have block scope, just function scope. They're just different languages and they do things differently.