Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function programmatically/"by string" in coffeescript [duplicate]

Possible Duplicate:
Dynamic object property name

I have a function that I need to call based on a user inputted data.

So for example, I have:

models.cat
models.dog

Now, I want to be able to call models.[my_str] where my_str = "snake". So the computer would think it is trying to execute models.snake. Is there a way to do this in javascript or better yet coffeescript?

like image 430
Alexis Avatar asked Jan 16 '13 22:01

Alexis


1 Answers

You should be able to call it like so:

models[my_str]();

This should work in both Javascript and Coffeescript.

like image 87
Marc Baumbach Avatar answered Nov 10 '22 16:11

Marc Baumbach