5

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexis
  • 23,545
  • 19
  • 104
  • 143

1 Answers1

14

You should be able to call it like so:

models[my_str]();

This should work in both Javascript and Coffeescript.

Marc Baumbach
  • 10,323
  • 2
  • 30
  • 45