x = 1
f = -> it + x
console.log f 1
x = 100
console.log f 1
What is printed to the console?
Is the value of x in the function "f" evaluated when the function is defined, or when it is called?
The answer is when it is run. This will print to the console:
If you look at the loop again, you will see when you call the function, the value of x is its value in the last iteration of the loop, that is to say, "3"
Thus all the functions you create are the same, all of them add their argument to "3".
If you want to do what you are thinking off, you must do:
let
fn[x] = -> it + x
fn[1](1)
Which in the next version of LiveScript will simply be, "for let ...."
This works because you are wrapping the body of the loop with "let", which is like wrapping it in an anonymous function and calling it with "x", so that the "x" in the function you are creating no longer refers to the mutating variable in the loop, but the parameter of the wrapping function ("let") which was just called with the current value of "x" in the loop.