Calling a javascript function encoded in a string

If I put the function in a string like this:

var functionString = function (message) { console.log(message); }.toString(); 

Is there a way to convert a string to a function and call it? I tried

 eval(functionString) 

which returns "Uncaught SyntaxError: Unexpected token" and

 functionString.call(this, "HI!"); 

which returns 'undefined is not a function'.

Is this possible in javascript?

Thanks in advance for any answer!

EDIT: The point of this question is that the function was converted to a string using toString (). So,

 console.log(functionString); 

returns this line: "function (message) {console.log (message);}"

Is it possible to convert a string back to a function and call it? This is the problem I'm trying to solve. Thanks!

+6
source share
4 answers

Your functionString contains exactly the string

"function (message) { console.log(message); }"

Assessing it as-is, there is a JavaScript engine with the wrong syntax (there is no name for this function). JavaScript expects a construct as function <name>(<params>) { } . Alternatively, you can use an anonymous function (i.e. the Name is missing), but only as a parameter or in the context of evaluating the expression. The minimum typical expression for evaluating is (function() {})() If you want a fantasy,! !function() {} also good - the exclamation mark in front turns it into a logical expression that requires evaluating the function before denying the output.

So in your example, this will work:

eval("("+functionString+")('abc')");

because then you make an anonymous function call - something that JavaScript can live with.

Alternatively, you can also use only parentheses, then you need to assign the result to what you can use later:

 var foo = eval("("+functionString+")"); foo('ddd'); 

Here's a little proof / playground to find out about this: http://jsfiddle.net/Exceeder/ydann6b3/

+2
source

You're almost there, but something is missing.

When we call toString () in your function, we get

 "function (message) { console.log(message); }" 

which we can then evaluate. However, we just create an anonymous function object here; we cannot name him!

If we instead do something like:

 var functionString = "var restoredFunc = " + function (message) { console.log(message); }.toString(); 

Then we can do the following

 eval(functionString); // prints "hello!" to console restoredFunc("hello!"); 
+3
source

yes its possible in javascript but you cannot eval anonymous function without assignment

So you go through it like this

 var functionString = function (message) { console.log(message); }.toString(); eval("myfunction =" + functionString) myfunction("Hello World!") 
+1
source

Your functionString is a string that looks like

 "function (message) { console.log(message); }" 

You can hide this string in an expression with instant call function (IIFE) using string concatenation as shown below.

 (function (message) { console.log(message); })("HI!"); 

and then eval. Here is the result from the Chrome JavaScript console:

 eval('(' + functionString + ')("HI!")') HI! 
+1
source

Source: https://habr.com/ru/post/979193/


All Articles