raganwald-deprecated / homoiconic

An experiment in publishing code and words about code on a small scale.
http://github.com/raganwald/homoiconic/tree/master/homoiconic.markdown
Other
1.82k stars 112 forks source link

Fix "do" example #22

Open joliss opened 11 years ago

joliss commented 11 years ago

The first do in the example is redundant (and requires methods to be null or undefined), and the other ones don't do anything because they're not taking parameters. Instead, you need a do (method) -> call, analogous to the IIFE ((function(method) { ... })(methods[i])) in the JS code.

Here's a demo.

class Frame

methods = null
do (methods = ['remove', 'show', 'hide', 'stop']) ->
  for method in methods then do ->
    Frame.prototype[method] = ->
      for i in [0...1] then do ->
        console.log method

(new Frame).remove() # => stop :(

methods = ['remove', 'show', 'hide', 'stop']
for method in methods
  do (method) ->
    Frame.prototype[method] = ->
      for i in [0...1]
        console.log method

(new Frame).remove() # => remove :)