In python I can put a function into a variable at runtime, then call it using the getattr function
method = getattr(self,self.name)
method()
Is there a similar way to do this in Coffeescript?
Thanks!
Using zeekay suggestion but using classes would be:
class Test
foo: -> alert 'foo'
foo2: methodName -> this[methodName]()
x = new Test
x.foo2('foo')
In Javascript objects are associative arrays, and you can access property/methods using the name of the property as the key:
Your updated example doesn’t work because
foo2is merely returningfoo. You might want to try this: