I have a context menu that returns the menu item clicked as a value in a variable, like so.
var m = key;
The variable can be anything I program, for example; edit, cut, paste or anything else I want.
Is it possible to use this variable as a function name? For example: function m() where m can be the content of the variable.
Any suggestions will be much appreciated.
Thanks
Chris
You could create an object with the functions you need and then use the variable to call the functions:
var funcs = {
"cut": function(){
console.log("Cutting");
},
"paste": function(){
console.log("Pasting");
}
};
var m = "cut";
funcs[m]();
http://jsfiddle.net/WVBNV/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With