1

I am quite new to javascript, but I know you can call a function with it being represented by a string such that:

 var function_to_call = window[values['function']];
 //Where values['function']='functionName'

So far so good, then we have:

 if(typeof function_to_call == 'function'){
       if(values['parameters']!= '')
                function_to_call(values['parameters']);
       else function_to_call();
  };

Of course, this won´t work because the parameters come out as "parameter1, parameter2" all in one string so you end up with

function_to_call("parameter1, parameter2");

rather than

function_to_call(parameter1, parameter2);

any ideas? Appreciate your time!

EXPANDED:

The parameters passed to the function represent the "id" of elements in the page; so the function that is called will be trying to get those elements by doing:

document.getElementById(parameter1);
...some other things...
document.getElementById(parameter2);
5
  • What data type is values['parameters']? Commented Jun 14, 2013 at 0:33
  • 1
    You probably need to parse the parameters string into parameter array first and then use function.apply() to call the function. Commented Jun 14, 2013 at 0:34
  • @Paul, a string such as: 'parameter1, parameter2'. Commented Jun 15, 2013 at 8:37
  • @bigbearzhu I´ll look into function.apply() since I don´t know what it is. Commented Jun 15, 2013 at 8:38
  • Got it woking with .apply(), thanks for the help! Commented Jun 15, 2013 at 16:03

2 Answers 2

3

I assume the parameter names represent global variables as well.

If so, you could split them into an Array, then .map() the Array into a new array of the related globals.

Then use .apply() to invoke the function with the array of arguments.

if (typeof function_to_call == 'function') {
     if(values['parameters']!= '') {
          var args = values['parameters'].split(",")
                                         .map(function(name) {
                                             return window[name.trim()];
                                         });
          function_to_call.apply(window, args);
     } else function_to_call();
}

The .trim() and .map() methods will need a shim for IE8... but this mainly shows how you could do it. As an alternative, you could pass a regex to .split() to take care of any spaces.

var args = values['parameters'].split(/\s*,\s*/)...
Sign up to request clarification or add additional context in comments.

9 Comments

' param, param2 '.trim().split(/,\s*/) is ["param", "param2"], saves you a loop. But what happens if you have a param that looks like "\"hello, world\""?
@PaulS.: Yes, but I think OP wants to resolve them to global variables. If not, then yes, the loop isn't needed.
@PaulS.: Those wouldn't be valid variables. They're valid properties when enclosed in a string, but I'm pretty sure OP is after variables here.
@CrazyTrain What exactly does "I assume the parameter names represent global variables as well." mean?I apologize but I am newbie with javascript, and I believe that that phrase is important because your example is not giving me the result I need.
@CrazyTrain I used .apply(), and parsed the string to array with .split(",") , and made sure that the parameters passed employed no spaces and only comma-separation so as to avoid .map() (to trim white spaces). thanks for the help!
|
0
 if(typeof function_to_call == 'function'){
   if(values['parameters']!= '')
            setTimeout('function_to_call('.values['parameters'].');');
   else  setTimeout('function_to_call();',0);
  }

1 Comment

Using setTimeout like this is an eval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.