Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a variable as a function name

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

like image 805
Chris Avatar asked Apr 01 '26 14:04

Chris


1 Answers

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/

like image 150
Kevin Bowersox Avatar answered Apr 03 '26 04:04

Kevin Bowersox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!