0

I have 2 JavaScript functions that dynamically-named..

function a1(){
   //some codes
}

function a2(){
   //some codes
}

function a3(){
   //some codes
}

these functions above generated by PHP for sure..

<?php 
  for($i = 1; $i <= $load_period->count; $i++){ 
?>
  <script type="text/javascript">
  function a<?php echo $i;?>(){
     //javascript functions
  }
  </script>
<?php 
  } 
?>

now I have to call both of these functions with for..

for(var i= 1; i <= 3; i++){
   // here the solution code .. 
   // doesn't work >> a+i+();
}
  • NB 1 : it's doesn't work with "a+i+();" .
  • NB 2 : I don't want to use "EVAL" syntax

thanks for any solution..

BenMorel
  • 34,448
  • 50
  • 182
  • 322
aswzen
  • 1,512
  • 29
  • 46
  • 2
    If you have to do this you're probably doing it wrong... – elclanrs Aug 14 '13 at 07:29
  • I suggest writing your code in a way that doesn't require this. You could easily achieve the same goal a different way. – Joe Simmons Aug 14 '13 at 07:29
  • possible duplicate of [How to find JavaScript variable by its name](http://stackoverflow.com/questions/724857/how-to-find-javascript-variable-by-its-name) and [Javascript dynamic variable name](http://stackoverflow.com/q/5117127/218196). – Felix Kling Aug 14 '13 at 07:35

3 Answers3

10

If the functions are globally accessible you may write

window['a'+i]()

Otherwise, you may be able to rewrite your code to add all the functions to one variable:

var f = {
    a1: function() { },
    a2: function() { },
    a3: function() { }
};

... and call f['a'+i]().

If you're rewriting, since all functions are named by an index, you might as well write:

var myFunctions = [
  function() { },
  function() { },
  function() { }
];

... and call myFunctions[i](). (Of course, as Felix Kling points out in comments, i here would have to be adjusted to be 0-based).

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • I like this approach best using a "wrapping" object. – DrColossos Aug 14 '13 at 07:30
  • im sorry ..but "window['a'+i]()" doesnt work too.. ive been searching all on another question in SO but got nothing – aswzen Aug 14 '13 at 07:39
  • @SiegitWisnuA: In that case your functions aren't globally accessible (unless you've done something wrong). If you can't rewrite your code, to either move your function declarations to a global scope, or to add a wrapping object of some sort, then your problem can't be solved without `eval`. – David Hedlund Aug 14 '13 at 07:42
  • @SiegitWisnuA: After your edit, it still looks like the functions are global, but the numbering is from `1` to `X`, not from `0` to `3`. If you start the loop from `0` (like you do in your code in JS), it will throw an error. – Felix Kling Aug 14 '13 at 07:45
  • @Siegit: Then you have to provide more information. With respect to the code you posted, `window['a'+i]()` should work fine. – Felix Kling Aug 14 '13 at 07:48
  • the codes actually are in.. $('#right_form').show(function(){ for(var i = 1;i <= 3;i++){ //solution } }); – aswzen Aug 14 '13 at 07:51
  • @DavidHedlund thanks before..im trying to rewrite the codes..about eval..that was the last option.. :) – aswzen Aug 14 '13 at 07:54
2

If they're global functions, you can do:

window['a' + i]()

Otherwise, you have to use eval (and frankly, this is a perfect use case for eval, because nothing else works):

eval('a' + i)()

JavaScript doesn't provide you with access to the variables in the current scope through an object (Python does this with locals() and globals()), so eval is your only choice if you cannot rewrite that code.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • 2
    @alexP: Don't parrot advice that you read somewhere on the internet. `eval` was included into the language to be used. There are better solutions 99% of the time, but `eval` can be used as a last resort. – Blender Aug 14 '13 at 07:32
  • Somewhere? [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2Feval#Don.27t_use_eval.21) – alexP Aug 14 '13 at 07:33
  • Technically, that would be somewhere on the internet. Just saying. – Joe Simmons Aug 14 '13 at 07:34
  • 1
    @alexP: How is my use of `eval` invalid? It introduces no security holes. Like I said, `eval` isn't the forbidden fruit. You can use it if you have a good reason to and because JavaScript doesn't provide you a way to access the variables in the current scope through an object, `eval` is your only choice. – Blender Aug 14 '13 at 07:35
  • Ok, you know that and I know that, but I always try to avoid the function 'eval' in forums. But you're right. – alexP Aug 14 '13 at 07:37
  • 1
    `eval` is like fire. A small kid is told that fire is really really bad. But when it grows up and understands how fire works and what it does, it is probably able to use it appropriately. (Who would want to forsake barbecue?) – Felix Kling Aug 14 '13 at 07:39
1

if the methods are in global scope then

for(var i= 1; i <= 3; i++){
    window['a' + i]()
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531