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
  • 34,719
  • 24
  • 185
  • 251
Alexis
  • 21,572
  • 18
  • 95
  • 139

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,103
  • 2
  • 29
  • 44