Given a String Describing a JavaScript Function, Convert It to a JavaScript Function

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

There's also the Function object.

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

Given a function in a string format how do i convert it back to a function in javascript?

You can give a name to the function, then you can call it. This should work

var fn = function () {
return 6 + 5;
};
var stringFunc = fn.toString();

eval("var newfn = " + stringFunc);
console.log(newfn())

you can also execute it passing the parameters like this

console.log(eval("("+stringFunc+")()"))

How to invoke a function that is given to me by string

By either:

 var fn = new Function( "myRequest, myResponse" , "myResponse.body = 'hello';myResponse.end();" );  

or by eval function which executes code directly from string:

    c = "function(myRequest,myResponse){myResponse.body = 'hello'; myResponse.end();}";
eval("var fn = "+c);

fn();

How to invoke a function that is given to me by string

By either:

 var fn = new Function( "myRequest, myResponse" , "myResponse.body = 'hello';myResponse.end();" );  

or by eval function which executes code directly from string:

    c = "function(myRequest,myResponse){myResponse.body = 'hello'; myResponse.end();}";
eval("var fn = "+c);

fn();

How to parse static text in Javascript

Yes, it is possible, but not a good idea. You use eval. This isn't the best to do for reasons too complex and opinion-based to get into here.

JavaScript: Function vs Variable

Yes it is done. Help came from this post:

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

I used user984003 answer. Thanks a lot user984003.

Here is the solution:

>s = window.document.createElement("script");
>s.innerHTML = cell._cell.row.data.jsfunction
"function toCelsius() {
return (5/9) * (200-32);
}"
>window.document.body.appendChild(s)
<script>​function toCelsius() {
return (5/9) * (200-32);
}​</script>​
>toCelsius()
93.33333333333334

Thanks to everybody

when the alert() in string , how to convert it as a function to show the alert in javascript

You should use the eval() function:

The eval() function evaluates or executes an argument.

If the argument is an expression, eval() evaluates the expression. If
the argument is one or more JavaScript statements, eval() executes the
statements.

var msg = "alert(\"welcome\")"; // Sample message.console.log(msg);             // Log to the console regardless.eval(msg);                    // Evaluate the message as an expression.// Alternatively, regarding your comment about the message being altered, you can do one of the following:var msg = "alert('welcome')";console.log(msg);eval(msg); // Or:var msg = 'alert("welcome")';console.log(msg);eval(msg); // Or even:var msg = 'alert(\'welcome\')';console.log(msg);eval(msg); 

Convert a string to a function, gives ReferenceError

In order to use the function m in that code string, you need to pass it as a parameter to the function:

const m = require(...);
const f = new Function("m", "m('p', {class:'red'}, 'text')");
f(m);


Related Topics



Leave a reply



Submit