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?

4
  • if you know the parent object (e.g. window) then you can do e.g. window['foo']() Commented Aug 3, 2016 at 2:20
  • 1
    have you tried using eval ? try eval(a_string); note eval is bad.. you should change your implementation Commented Aug 3, 2016 at 2:20
  • Possible? Maybe. A reasonable solution to anything? Almost never. Commented Aug 3, 2016 at 2:21
  • 1
    Possible duplicate of How to execute a JavaScript function when I have its name as a string Commented Aug 3, 2016 at 2:26

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

@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
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.
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);

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.