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
Case Insensitive String Replacement in JavaScript
How to Request the Garbage Collector in Node.Js to Run
Get Character Value from Keycode in JavaScript... Then Trim
Put JavaScript in One .Js File or Break It Out into Multiple .Js Files
How to Ignore User's Time Zone and Force Date() Use Specific Time Zone
Why Does Changing One Array Alters the Other
Detect When a Window Is Resized Using JavaScript
Convert an Image into Binary Data in JavaScript
Get Keys of JSON-Object in JavaScript
How to Use Blob Url, Mediasource or Other Methods to Play Concatenated Blobs of Media Fragments
Es6 Promises - Something Like Async.Each
How to Listen for a Click-And-Hold in Jquery
How to Make an Angularjs Directive to Stoppropagation
Why JavaScript Functions Always Return a Value