2

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

Bort
  • 7,398
  • 3
  • 33
  • 48
Chris
  • 309
  • 1
  • 3
  • 14

3 Answers3

9

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/

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • 1
    Good idea to not use `window` to store the functions ;-) – John Dvorak Dec 14 '12 at 19:03
  • Is there any benefit to quoting the literal keys even when not needed? – John Dvorak Dec 14 '12 at 19:04
  • @Jan Dvorak, I mainly do it because I have gotten burned using `class`, which is a reserved word too many times (IE doesn't like this). But I believe it is also part of the spec: http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes – Kevin Bowersox Dec 14 '12 at 19:05
  • @KevinBowersox the spec refers to JSON, not to object literals. I agree with the caution about keywords. – John Dvorak Dec 14 '12 at 19:07
0

Sure can!

var onComplete = fadeOut;

function fadeOut(){
   $(this).animate({
      opacity: "0"
   },400);
}

Coincidentally, you can also use this a way to pseudo make classes.

function myClass(){
    this.publicVar = "" //<-- this is a public var
    var privateVar = ""; //<-- this is private

   this.myPublicMethod = myPublicMethod;

   function myPublicMethod(){ // <-- public because of the line above

   } 

   function _myPrivateMethod(){ // <-- if just this it's private

   }

}
  • 1
    Could be wrong but you're assigning `fadeOut` to `onComplete` before you define `fadeOut` so I don't think this will work. Not to mention this doesn't relate to this question at all. – War10ck Dec 14 '12 at 19:09
  • Incorrect, I'm assigning it to memory but it should be called after the fact. Comment out the last line and it'll work. http://jsfiddle.net/biscuitcleaver/kmgvn/ The second block of code is to extrapolate on the first one. You can assign variables to functions to expose them out of the original function to make an OO type of approach. – biscuitcleaver Dec 14 '12 at 19:33
0

Another way

function my_generic_function(m) {
    switch (m) {
    case "cut":
        console.log("cutting");
        break;
    case "copy":
        console.log("copying");
        break;
    default:
        return false;

    }
}

var m = "cut";
my_generic_function(m);

var m = "copy";
my_generic_function(m);
​
Atif
  • 10,623
  • 20
  • 63
  • 96
  • I like your answer. I am new to jquery, so simple works best for me. Its also sort of self documenting. – Chris Dec 14 '12 at 19:15
  • @Chris The both my solution and Atif's are using pure javascript. I would urge you to learn the basics of JS before moving to jquery. – Kevin Bowersox Dec 14 '12 at 19:55