Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass function in json and execute

Is there any way that I can pass a function as a json string (conversion with JSON.stringify), send it to another function, parse the json and then execute the function that was in the json? I am using jquery and javascript.

like image 806
amateur Avatar asked Oct 15 '10 23:10

amateur


People also ask

Can you pass function in JSON?

Yes, you can.

Can we write functions in JSON file?

"There is no way to store functions in JSON." — That isn't true. That just shows there is no native function data type. A function could be expressed as a string.


1 Answers

Yes, you can. There are tons of ways to do it.

And there is no need to use the "evil" eval function (please yahoogle why it should be avoided) as pointed out here: http://javascript.about.com/library/bleval.htm

var tmpFunc = new Function(codeToRun); tmpFunc();  

Whether it was JSON at any stage should be irrelevant.

like image 130
EasierSaidThanDone Avatar answered Oct 05 '22 15:10

EasierSaidThanDone