0

I would like to have a myFunction run another function which has a previously defined argument in its name being passed through myFunction.

I currently have

function myFunction(SelectedImageNumber)
{
alert(SelectedImageNumber);
}

passing the image's number (1 - 47), and it announces in an alert.

Instead of an alert, I would like it to run a function by the name of

selectImage1(){
stuff;
}

selectImage2(){
other stuff;
}

...

selectImage47(){
even more stuff;
}

I'm learning as I go, and don't have a lot of javascript experience... Thanks

GuessWho
  • 3
  • 1
  • 3
    Any time you need to do something like this, take a step back and realize that you probably should be using a list/array instead. – Matt Ball Jul 19 '13 at 01:25
  • I think the learning here is that you shouldn't do it like you are trying to do. – mohkhan Jul 19 '13 at 01:27
  • 1
    Do you really intend to write 47 different functions? As Matt Ball sad, you should use arrays or just one function passing the image as a parameter. – Lucas Maus Jul 19 '13 at 01:27
  • The pattern can be useful as a way of injecting code into your function – TGH Jul 19 '13 at 01:29
  • I entirely agree with previous commenters: programming is partially about being *lazy*; as in, not writing tedious and difficult to extend code. – user2246674 Jul 19 '13 at 01:30
  • I really don't know enough about this to know how to write it out as an array either. Where could I go to learn about that? Thanks for pointing me in a better direction. – GuessWho Jul 19 '13 at 01:35
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – Matt Ball Jul 19 '13 at 01:39
  • 1
    @GuessWho If you provide a sample of what the code *inside* the function looks like, you will likely get more relevant answers .. the idea is to first *generalize* and then *specialize* if needed. Starting off with identifiers (e.g. function names) x1, x2 .. xN misses out on the important generalization phase. – user2246674 Jul 19 '13 at 01:41
  • When you're going to write code that creates those 47 function for you, you should have a look at http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example – Bergi Jul 19 '13 at 01:47

6 Answers6

0

This pattern will let you inject code into another function.

var selectImage1 = function(){};
var selectImage2 = function(){};
var selectImage3 = function(){};

function RunOtherFunction(f)
{
  f();
}

//call it like this

RunOtherFunction(selectImage1);
...
RunOtherFunction(selectImageN);

Other implmentation:

I will however, agree with those who argue that it's crazy to define 42 functions to do very similar work. The selectImage1...selectImageN suggests that there is some convention at play here. Maybe you can consolidate everything into a single function based on the convention and some simple logic. We need to see what selectImage(n) does though in order to suggest ways to consildate

TGH
  • 38,769
  • 12
  • 102
  • 135
  • Making 47 functions to do what the OP is trying to do is just silly. He wants to learn. Teach him how to do it better. That's why. While this does complete his answer, I believe he just shouldn't do it this way. – Shawn31313 Jul 19 '13 at 01:32
  • That's a different matter. Illustrating how functions are first class citizens in java script is a very valuable lesson, one that my answer teaches you. In other scenarios you might want to pass in very different implementations for different scenarios, which would make this apprpriate. – TGH Jul 19 '13 at 01:36
  • Ha, okay. That's fair! – Shawn31313 Jul 19 '13 at 01:37
  • How would that work? the 1-47 in *selectImage* are being pulled from yet another function and being passed to *myFunction(SelectedImage)* – GuessWho Jul 19 '13 at 01:38
0

Like others said, there is something definitely that needs to be relooked. More than likely you should be able to squeeze all 47 functions into a single function. Having said that, just to answer your question, functions in javascript are first class objects and hence you can do anything and everything with them, that you can do with objects. Relevant point here being you can pass them as parameters. Something like below should serve your purpose.

function (funcToRun) {
   return funcToRun && funcToRun();
}

Note that if you needed the parameter function to take some parameters, you can pass anything that is global, or in closure scope (param or variables local to the parent function)

0

Since functions in JavaScript are objects, you can list them in an array:

var myFuncs = [
  function one(){},
  function two(){},
  function three(){},
  ...
];

Then you can make a function that calls them by index:

// assuming n starts at 1
function myFunction(n) {
  myFuncs[n-1](); // n=1 calls function "one"
}

But, I think this is probably unnecesary, you should handle the logic in one function and decide what to do there:

function selectImage(n) {
  switch(n) {
    case 1:
      // do something if n==1
      break;
    case 2:
      // do something if n==2
      break;
    ...
  }
}

The idea is to abstract the logic until you find a common pattern. Even the switch statement is probably too long and could be simplified, but this is better than 47 functions.

elclanrs
  • 92,861
  • 21
  • 134
  • 171
0

Like others have said, you probably don't want to do it like this. However, if for some bizarre reason you HAD to, you could simply make an array of functions and call the one that corresponded to the number you passed in, like so:

var selectImage1 = function() {...}
...

var array_of_functions = [
  selectImage1,
  selectImage2,
  selectImage3,
  ...
  selectImage47
]

function myFunction(selectedImageNumber)
{
  array_of_functions[selectedImageNumber-1]();
  // The offset is because you didn't zero-index the image functions.
}
Chase Sandmann
  • 4,795
  • 3
  • 30
  • 42
0

Why not make an object?

var selectImage = function(number) {
    var number = number;

    function foo() {
        alert(number);
    }

    return {
        'foo': foo
    };
 };

function somefunction (selectedImage) {
    selectedImage.foo();
}

somefunction(selectImage(1));

Of course you'd have to know the inner workings of the object. Or maybe I'm over-thinking what you want to do?

Ford
  • 2,559
  • 1
  • 22
  • 27
-1
function selectImageByIndex(index){
    selectImage[index]();
}

var selectImage = {
    "1": function(){
        alert(1);
    },
    "27": function(){
        alert(27);
    }
}
selectImageByIndex(27);  //displays "27"
aim
  • 154
  • 7