Hacker News new | ask | show | jobs
by geraldalewis 5015 days ago
Also, there's a little issue with @raganwald's counterexample CoffeeScript code. Here's a working version:

    methods = ['remove', 'show', 'hide', 'stop']
    for method in methods 
      do (method) ->    # *see notes below
        Frame::[method] = ->
          element[method]() for element in @elements

*As @jashkenas points out, under the hood, `do` creates a new function (and thus a new scope). Invoking `do` with a parameter list (here "(method)") takes a variable from the outer scope and uses it for a parameter name within the newly created scope. So within that new function, references to `method` will not refer to the `method` variable of the loop body, but to the new function's `method` parameter. `do (method) ->` basically compiles to: `(function(method){ ... })(method)`.

See https://gist.github.com/3742581.