Given a string describing a Javascript function, convert it to a Javascript function

Javascript

Javascript Problem Overview


Say I've got a Javascript string like the following

var fnStr = "function(){blah1;blah2;blah3; }" ;

(This may be from an expression the user has typed in, duly sanitized, or it may be the result of some symbolic computation. It really doesn't matter).

I want to define fn as if the following line was in my code:

var fn = function(){blah1;blah2;blah3; } ;

How do I do that?

The best I've come up with is the following:

var fn = eval("var f = function(){ return "+fnStr+";}; f() ;") ;

This seems to do the trick, even though it uses the dreaded eval(), and uses a slightly convoluted argument. Can I do better? I.e. either not use eval(), or supply it with a simpler argument?

Javascript Solutions


Solution 1 - Javascript

There's also the Function object.

var adder = new Function("a", "b", "return a + b");

Solution 2 - Javascript

You can do this:

//in your case: eval("var fn = " + fnStr);
eval("var fn = function(){ blah1;blah2;blah3; }"); 
fn();

Not sure how to get it much simpler, sometimes there's no (better) way around eval(). Here's a quick example of this in action.

Solution 3 - Javascript

Use parentheses.

var fn = eval("(function() {...})");

This technique is also good for transmitting JSON values.

By the way, it's often better to build functions by composing them directly from other functions. If you are using strings, you have to worry about things like unexpected variable capture.

Solution 4 - Javascript

Here's what I use for simple cases:

// an example function
function plus(...args) {
    return args.reduce( (s,v) => s+v, 0 );
}

// function to string
let str = plus.toString();

// string to function
let copy = new Function('return ' + str)();

// tests
console.assert(plus.name == 'plus');
console.assert(copy.name == 'plus');
console.assert(plus.constructor == Function);
console.assert(copy.constructor == Function);
console.assert(plus(1,2,3,4) === copy(1,2,3,4));
console.assert(plus.toString() === copy.toString());

Solution 5 - Javascript

You can also insert the string into a script element and then insert the script element into the page.

script_ele = window.document.createElement("script");
script_ele.innerHTML = 'function my_function(){alert("hi!");}';
window.document.body.appendChild(script_ele);
my_function();

Solution 6 - Javascript

The Function constructor creates a new Function object. In JavaScript every function is actually a Function object.

// Create a function that takes two arguments and returns the sum of those arguments
var fun = new Function("a", "b", "return a + b");
// Call the function
fun(2, 6);
Output: 8

Solution 7 - Javascript

One way:

     var a = 'function f(){ alert(111); } function d(){ alert(222);}';  
     eval(a);
     d();
     

A second more secure way to convert string to a function:

     // function name and parameters to pass
    var fnstring = "runMe";
    var fnparams = ["aaa", "bbbb", "ccc"];
    
    // find object
    var fn = window[fnstring];
    
    // is object a function?
    if (typeof fn === "function") fn.apply(null, fnparams);
    
    
    function runMe(a,b){
      alert(b);
    }

Look at the working code: http://plnkr.co/edit/OiQAVd9DMV2PfK0NG9vk

Solution 8 - Javascript

You can call Parse your string as javascript fuction

  1. function getDate(){alert('done')} // suppose this is your defined function

to call above function getDate() is this in string format like 'getDate()'

  1. var callFunc=new Function('getDate()') //Parse and register your function

  2. callFunc() // Call the function

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbrainjamView Question on Stackoverflow
Solution 1 - JavascriptgoatView Answer on Stackoverflow
Solution 2 - JavascriptNick CraverView Answer on Stackoverflow
Solution 3 - JavascriptEricView Answer on Stackoverflow
Solution 4 - JavascriptJulian TFView Answer on Stackoverflow
Solution 5 - Javascriptuser984003View Answer on Stackoverflow
Solution 6 - JavascriptSimpal KumarView Answer on Stackoverflow
Solution 7 - JavascriptArthur TsidkilovView Answer on Stackoverflow
Solution 8 - JavascriptSatish Kumar sonkerView Answer on Stackoverflow