Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call javascript function encoded in a string

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!

like image 298
user1840267 Avatar asked Dec 05 '14 21:12

user1840267


People also ask

How do you call a JavaScript function from a string?

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.

What is JavaScript string encoding?

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.

How do I use encodeURI in JavaScript?

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).

What is $() in JavaScript?

$() 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.


3 Answers

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!");
like image 175
Aaron Hammond Avatar answered Oct 13 '22 01:10

Aaron Hammond


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/

like image 44
Alex Pakka Avatar answered Oct 13 '22 01:10

Alex Pakka


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!")
like image 30
tinker Avatar answered Oct 13 '22 01:10

tinker