I want execute javascript function which the name is coming as a string dynamically. ansd i dont need to pass any parameters while executing the function.
Please can any one guide me how to achieve this?
Regards, Kamesh
I want execute javascript function which the name is coming as a string dynamically. ansd i dont need to pass any parameters while executing the function. Please can any one guide me how to achieve this? Regards, Kamesh |
|||||||||
|
one simple way
or
|
|||
|
dangerous but you could use |
|||||
|
are you talking about ´eval()´??
Hope this helps; |
|||
|
function a() { console.log('yeah!'); } var funcName = 'a'; // get function name this[funcName](); |
|||
|
If the function is global, you should do |
|||
Using You can use
This way you can optionally add arguments as well |
|||
|
Perhaps a safer way is to do something like this (pseudo code only here):
You might use a switch statement for this (it seems like a better construct):
You can optimize this by creating an array of function names, searching the array for the desired function name, then executing the value in the array. |
||||
|