Your internal function:
(args...) -> method = v console.log('executing method: ' + method)
is actually a closure on v , so when it is executed, v will evaluate its last value in the loop (i.e. set ). Looks like you just need to sort out the closure a bit:
build_method = (v) -> (args...) -> method = v console.log('executing method: ' + method)
And then:
for k, v of ['get', 'set'] console.log('creating method: ' + v) MyClass::[v] = build_method(v)
Keep in mind that CoffeeScript is just JavaScript with a different make-up, so it experiences a lot of problems (like any language with closure), and these problems have the same solutions.
source share