📎 Webclip
Closures: Elixir vs. Ruby vs. JavaScript
The page argues that closures behave very differently across JavaScript, Ruby, and Elixir. In JavaScript, mutable state can produce unexpected results when a closure captures a loop variable. Ruby avoids that in common iterator patterns, but the same issue can still appear with bare language constructs. Elixir is presented as the safest of the three because immutable data structures prevent the captured value from changing later.
Reading notes#
- JavaScript closures keep the environment where they were defined, so a loop variable can end up with the last mutated value in every function call.
- The usual workaround in JavaScript is to wrap the anonymous function in another anonymous function to create a new context.
- Ruby’s built-in iterators can avoid the problem, but the same closure issue appears when using simpler language constructs.
- Elixir uses immutable data structures, so reusing a variable label does not change the earlier values that closures reference.
- The author recommends using iterators in JavaScript and Ruby and says functional programming habits can reduce headaches.
- The page ends by encouraging readers to learn Elixir.
