0

Is it possible to run a function whose name is saved in a string?

For example: I have three functions:

function nr1(p0, p1){
    /* [...] */
}
function nr2(p0, p1){
    /* [...] */
}
function nr3(p0, p1){
    /* [...] */
}

and I have a string whose content is a function name, for example:

var a_string = "nr2(27, 42)";

Is it possible to execute the function whose name is stored in the string?

scniro
  • 16,844
  • 8
  • 62
  • 106
niklassc
  • 597
  • 1
  • 9
  • 24
  • if you know the parent object (e.g. `window`) then you can do e.g. `window['foo']()` – CrayonViolent Aug 03 '16 at 02:20
  • 1
    have you tried using `eval` ? try `eval(a_string);` note [eval is bad](http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea).. you should change your implementation – Jairo Malanay Aug 03 '16 at 02:20
  • Possible? Maybe. A reasonable solution to anything? Almost never. – Alexander O'Mara Aug 03 '16 at 02:21
  • 1
    Possible duplicate of [How to execute a JavaScript function when I have its name as a string](http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string) – jon_athan_hall Aug 03 '16 at 02:26

3 Answers3

1

you can eval() it. Observe the following example...

function something() {
    console.log('did');
} 

var str = 'something()'

eval(str) // did

As comments suggest, not the best idea to take and run with - though it works... To expand on that just a bit more, I found this blog post which shares some good pointers on the somewhat controversial usage of this technique: eval() isn’t evil, just misunderstood


JSFiddle - simple demo

scniro
  • 16,844
  • 8
  • 62
  • 106
  • 1
    @EisregenGruss36 you are most welcome! I also included a link to a blog post I felt was pretty interesting which should answer anything you'd like to know about `eval` – scniro Aug 03 '16 at 02:31
  • Thanks for your response. I tried it and it works. I'm aware that it is probably not the best way to do it but I don't know how else I could do it. I am using it in Electron to send a String from the renderer process to the main process, in order to be able to execute main-process-functions from the renderer process. – niklassc Aug 03 '16 at 02:32
1

You can do eval, although it is frowned upon because of it's vulnerabilities. Something you could do is find it in the window object and then execute:

window["functionName"](args);

Calling with eval:

function test() { 
    console.log('test');
}

var fnstring = "test()";
eval(fnstring);
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
1

you can use eval to evaluates JavaScript code represented as a string. eval(a_string) but

eval is bad

  • Improper use of eval opens up your code for injection attacks
  • Debugging can be more challenging (no line numbers, etc.)
  • eval'd code executes more slowly (no opportunity to compile/cache eval'd code)

you better change your implementation instead of saving a string function call

function nr1(p0, p1){
    console.log('nr1 p0: ', p0);
    console.log('nr1 p1: ', p1);
}

function nr2(p0, p1){
    console.log('nr2 p0: ', p0);
    console.log('nr2 p1: ', p1);
}

function nr3(p0, p1){
    console.log('nr3 p0: ', p0);
    console.log('nr3 p1: ', p1);
}

var a_string = "nr2(27, 42)";

eval(a_string);
Community
  • 1
  • 1
Jairo Malanay
  • 1,327
  • 9
  • 13