admin管理员组

文章数量:1389825

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!

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!

Share Improve this question edited Dec 5, 2014 at 21:19 user1840267 asked Dec 5, 2014 at 21:09 user1840267user1840267 4281 gold badge6 silver badges20 bronze badges 7
  • It would help if you'd post exactly what's in the string. – Pointy Commented Dec 5, 2014 at 21:09
  • You could just do "return message.toString()" inside the function – ryanc1256 Commented Dec 5, 2014 at 21:12
  • Is it possible, yes it can be, but what are you trying to do exactly? – epascarello Commented Dec 5, 2014 at 21:14
  • stackoverflow./questions/912596/… – Brian Shamblen Commented Dec 5, 2014 at 21:19
  • epascarello, I would like to convert the string back to a function. Brian, I don't want to call an existing function. I would like to convert the entire string into a function. – user1840267 Commented Dec 5, 2014 at 21:20
 |  Show 2 more ments

4 Answers 4

Reset to default 3

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/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!")

Your functionString is a string that looks like

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

You could covert that string to an Immediately-Invoked Function Expression (IIFE) using string concatenation, similar to below.

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

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

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

本文标签: Call javascript function encoded in a stringStack Overflow