0

I am trying to build a lib and I need to call functions dynamically depending on the variables I have in parameter like this

strategies = min
function dispatchRuleToStrategy(strategies)
{
    $.each(strategies, function(index, value) {
        strategy = "strategy_" + value;
    });
}

function strategy_min()
{
    // do something
}

How can I call the function strategy_min() from dispatchRuleToStrategy()?

I've been trying a couple of things none of which are working.

Thanks for your help

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
khalid sookia
  • 317
  • 2
  • 13

5 Answers5

6

Use an Object to create a dictionary of your functions e.g. lib

var lib = {
    'strategy_min': strategy_min
};

then you can invoke via the key in this dictionary Object

lib['strategy_min']();

If you've named all your functions and you don't want to re-type the names over and over, you could

var lib = {};
function addToLib(fn) {
    lib[fn.name] = fn;
}
// then
addToLib(strategy_min);
// or
[strategy_min].forEach(addToLib);
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • hi, i've been trying to get this to work for more than an hour now but it is not. The first one with everything hard coded but when i try to do it the second way it is not working. Here is what i did `function dispatchRuleToStrategy(strategies) { console.log(strategies); var lib = {}; function addToLib(fn) { lib[fn.name] = fn; } $.each(strategies, function(index, value) { addToLib("strategy_" + value); }); } function strategy_min() { console.log("ok min"); } function strategy_required() { console.log("ok required"); }` – khalid sookia Feb 04 '15 at 19:24
  • I've figured out what the problem is. The addToLib() cannot accept a string and I've only got a string. How can I bypass this problem ? – khalid sookia Feb 04 '15 at 21:56
  • @khalidsookia Yes, `addToLib` expects a _function_ it is a method to add a _function_ as a property of `lib` where the name of the property is the name of the function; I was showing you alternative ways to construct the `lib` _Object_ in my second code block, the invocation method would remain `lib['strategy_min']();` as before. The advantages of using `addToLib` are simply that you don't have to hard-code your `lib` _Object_ in the same way, making it easier to update or change in future. – Paul S. Feb 05 '15 at 02:29
2

Put them in an object and use the property name:

var strategy_table = {
    min: function() {
        // do something
    },
    max: function() {
        // do something else
    },
    ...
};

Then you can access them as strategy_table[value]:

$.each(strategies, function(index, value) {
    strategy_table[value]();
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Others have already suggested to create a wrapper object for the functions, however, if your strategy_min() function is in the global scope, you can access it directly:

window['strategy_' + value]();

window in browsers refers to the global object. The bracket notation is used to access properties whose keys are dynamically generated. This way you are accessing the function, which is a property of the global object, i.e. window, and calling it using the parentheses.

rhino
  • 13,543
  • 9
  • 37
  • 39
  • Use of the global namespace shouldn't really be encouraged and if you rely on it to achieve this, you can't easily restructure your code to stop using it – Paul S. Feb 04 '15 at 17:09
0

Finally I found the real problem. I was in a jquery document ready which is a closure. I did not knew what closures were before today.

Thanks all for your help

khalid sookia
  • 317
  • 2
  • 13
-3

You can use eval() function in the following manner

 $.each(strategies, function(index, value) {
    strategy = "strategy_" + value;
    eval(strategy+"()");
});
Hemant
  • 1,999
  • 2
  • 12
  • 8