SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check and call dynamic function name without eval?

    I have an array consists function name:
    myarray[1]['funcname']='func1()';
    myarray[2]['funcname']='func_abc()';

    Questions:
    1. how to check if the respective function exists? if I know the exact function name I can do:
    Code:
    if (func_abc) ...
    so I think I can use eval:
    Code:
    if (eval(myarray[2]['funcname'])) ...
    but I know eval is not good, so is there another way?

    2. what the best way to call the function? I've read some articles, they say we can call it using: variable_consists_functionname() but applying to my case return a quite strange statement and seems not working :
    Code:
    myarray[2]['funcname']();
    so what's the best way to call a dynamic function name?
    Last edited by explorer11; Feb 8, 2007 at 09:06. Reason: mistype

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about:
    Code:
    myarray[1]['func']=func1;
    myarray[2]['func']=func_abc;
    
    ...
    
    myarray[2].func();

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if (eval(myarray[2]['funcname']) )
    That is the same as writing:
    Code:
    if (eval('func1()') )
    which is the same as:
    Code:
    if (func1() )
    which can evaluate to false depending on what the return value for func1() is.

    If you have to store the elements of the array as strings with the format 'func1()', you could get a substring before trying to evaluate if the function exists:
    Code:
    myarray=[ [], [], [] ];  //create a 2d array
    myarray[1]['joe']='someFunc()';
    myarray[2]['sally']='anotherFunc()';
    
    var str = myarray[1]['joe'];  //shorten the reference
    
    var pos = str.lastIndexOf( '(' );
    var fname = str.substring(0, pos);
    
    if(window[fname])
    	alert("callable");
    else
    	alert("not callable");
    
    
    
    function someFunc()
    {
    	
    }
    
    function anotherFunc()
    {
    	return 10;
    }
    what the best way to call the function? I've read some articles, they say we can call it using: variable_consists_functionname() but applying to my case return a quite strange statement and seems not working:
    Code:
    myarray[2]['funcname']()
    That is the same as writing:

    'func_abc()' ();

    and you can't execute a string. A string is not a function. You can do this:
    Code:
    function someFunc()
    {
    	alert("hello");
    }
    
    var f = someFunc;
    f();
    
    //or this:
    
    var str = 'someFunc';
    window[str]();
    what's the best way to call a dynamic function name?
    You would have to give an example of what you mean by a "dynamic function name".
    Last edited by 7stud; Feb 8, 2007 at 14:59.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah... my bad, I simplified the problem but made wrong examples
    this one:
    Code:
    myarray[1]['funcname']='func1()';
    myarray[2]['funcname']='func_abc()';
    SHOULD BE:
    Code:
    myarray[1]['funcname']='func1';
    myarray[2]['funcname']='func_abc';
    @jimfraser:
    Actually I modified a script where the function names are assigned to array like above (using string), so if I changed it by assigning the function itself I think I have to change all the current codes. Anyway your code is a nice trick, thanks

    @7stud:
    yeah, after I read your reply I realize I gave a bad example, sorry...
    so, I think the answer is using window[fname] -> this one what I need!

    You would have to give an example of what you mean by a "dynamic function name".
    What I meant by "dynamic function name" is I don't know the name of the function until runtime, because it's passed from other script, so the function name is kept on a variable. However, you already answered it , thanks for your complete explanation

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the functions are stored as strings then use 7stud's method of
    window[myarray[1]['funcname']]();

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, I have tried that way and it's working

    thanks all


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •