If I put a function into a string like this:
var functionString = function (message) {
console.log(message);
}.toString();
Is there any way to convert the string back 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 that even possible in javascript?
Thanks in advance for any reply!
EDIT: The point of this question is that the function has been converted into a string using toString(). So
console.log(functionString);
returns this string: "function (message) {console.log(message);}"
Can I transform the string back into a function and call it? That's the problem I am trying to solve. Thanks!
There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.
A string is a series of bytes. A byte is 8 bits, each of which can be 0 or 1, so a byte can have 28 or 256 different values. Encoding is the process of squashing the graphics you see on screen, say, 世 - into actual bytes.
The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
$() The $() function is shorthand for the getElementByID method, which, as noted above, returns the ID of a specific element of an HTML DOM. It's frequently used for manipulating elements in a document. $() allows for shorter and more efficient JavaScript coding.
You're nearly there, but you're missing something.
When we call toString() on your function, we get
"function (message) {
console.log(message);
}"
which we can then eval. However, we're just creating an anonymous function object here; we won't be able to call it!
If we instead to something like:
var functionString = "var restoredFunc = " + function (message) {
console.log(message);
}.toString();
We can then do the following
eval(functionString);
// prints "hello!" to console
restoredFunc("hello!");
Your functionString
contains exactly the string
"function (message) { console.log(message); }"
Evaluating it as-is does present JavaScript engine with incorrect syntax (there is no name for this function). JavaScript expects construct like function <name>(<params>) { }
. Alternatively, you can use anonymous function (i.e. no name present), but only as a parameter or in a context of evaluating expression. The minimal typical evaluating expression would be (function() {})()
If you want to get fancy, !function() {}
is also ok - the exclamation mark in front turns it into boolean expression that requires function evaluation before negating the output.
So, in your example this will work:
eval("("+functionString+")('abc')");
because then you do anonymous function call - something JavaScript can live with.
Alternatively, you can also use just brackets, then you need to assign the result to something you can use later:
var foo = eval("("+functionString+")");
foo('ddd');
Here is a little proof / playground to learn about it: http://jsfiddle.net/Exceeder/ydann6b3/
yes its possible in JavaScript but you can't eval
anonymous function without assignment
So you go through it like so
var functionString = function (message) {
console.log(message);
}.toString();
eval("myfunction =" + functionString)
myfunction("Hello World!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With