I asked a question about currying and closures were mentioned. What is a closure? How does it relate to currying?
The closure object here is the value of the envelope variable and it's use is that it's a param to the each method. Some details: Scope: The scope of a closure is the data and members that can be accessed within it. A closure is a pairing of: A function and A reference to that function's outer scope (lexical environment) A lexical environment is part of every execution context (stack frame) and is a map between identifiers (i.e. local variable names) and values.
closure of project, Every function in JavaScript maintains a reference to its outer lexical environment. This reference is used to configure the execution context ... When you create the closure, i is a reference to the variable defined in the outside scope, not a copy of it as it was when you created the closure. It will be evaluated at the time of execution. Most of the other answers provide ways to work around by creating another variable that won't change the value for you.
closure of project, How to ensure closures created in a loop capture the value of the loop ... A closure can actually be any function within another function, and its key characteristic is that it has access to the scope of the parent function including it's variables and methods. And here comes the closure part: The closure of a lambda expression is this particular set of symbols defined in the outer context (environment) that give values to the free symbols in this expression, making them non-free anymore. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.