I have the name of a function in JavaScript as a string. How do I convert that into a function pointer so I can call it later?
Depending on the circumstances, I may need to pass various arguments into the method too.
Some of the functions may take the form of namespace.namespace.function(args[...]).
Don’t use
evalunless you absolutely, positively have no other choice.As has been mentioned, using something like this would be the best way to do it:
That, however, will not work with a namespace’d function:
This is how you would do that:
In order to make that easier and provide some flexibility, here is a convenience function:
You would call it like so:
Note, you can pass in whatever context you want, so this would do the same as above: