You need to close the loop value if you want to generate functions to run later. This coffee provides the do
keyword.
for food in foods do (food) -> Person.eat -> console.log food
See this example: https://gist.github.com/c8329fdec424de9c57ca
This is because the body of the loop has a reference to the variable food
, which changes the values ββeach time, although the cycle, and when you work, if it finds a closure, the function was created and finds that the food variable is set to the last value of the array. Using another function to create a new area solves the problem.
source share