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[...])
.
Easiest way is to access it like has element
is same as
This is working for me:
I hope this works.
Without using
eval('function()')
you could to create a new function usingnew Function(strName)
. The below code was tested using FF, Chrome, IE.With ES6 you could to access class methods by name:
the output would be:
You can call javascript function within the
eval("functionname as string")
either. Like below: (eval is pure javascript function)Working example: https://jsfiddle.net/suatatan/24ms0fna/4/