Evaluating functions for transfer in IE8


I need to transfer JavaScript Objects through JSON and transfer it's functions as well. I found a working version of getting the Strings of Functions and transfering them. Then i can evaluate them again.

//Create the function var myFunction = function(){alert('Hello, my Function!');} //get the functions String reprensentation var myFunctionTransferString = myFunction.toString(); //Now we transfered the object and want to have a function back var myTransferedFunction = eval('(' + myFunctionTransferString + ')'); //call the transfered function myTransferedFunction();

Here i have a jsfiddle for this: <a href="http://jsfiddle.net/bMjug/" rel="nofollow">http://jsfiddle.net/bMjug/</a>

This is working in Firefox, Chrome, and Safari as it should (but as you can guess not in that great pieace of microsoft software called Internet Explorer).

At the line where i want to evaluate the function i get the message fn is null or not an object in IE8.

Actually i found a solution for this but i really don't like this solution. If i put the variable declaration into the String i'm evaluating and remove the parantheses because i'm not expecting an object anymore that would do what i want:

eval('var myTransferedFunction = ' + myFunctionTransferString);

But i find this kind of hacked and bad solution.

Does anyone now a better one for this problem?

Thanks in advance


For what it's worth, the problem is caused by JScript incorrectly interpreting this:

(function x() {})

as a FunctionDeclaration and not a FunctionExpression, so doing a statement-eval instead of an expression-eval. Similar to what happens with {} object-literals without the wrapping brackets. You could get around it by doing something to more explicitly push it into parsing an expression, eg:


But <strong>seriously don't</strong>. Never rely on the string representation of a function, it is not standardised and there are <a href="http://perfectionkills.com/those-tricky-functions/" rel="nofollow">many browser differences</a>.

You couldn't usefully preserve a function even if function decomposition were reliable, since there is much more to a function than the textual representation of its source. The closures it includes cannot be represented, and closures are ever-more common in real-world JavaScript.

I'm afraid there is no cheap way to serialise/re-instantiate JavaScript objects in general. The JSON subset is the reliable subset, the rest you'll have to code your own ad hoc serialisation formats for.


Functions are not part of the JSON specification. Remember the JSON specification is a subset of JavaScript's syntax.

So your 'hacked' solution is actually the more correct one.


Heres some <em>hacked</em> solutions:

var fn = function() { alert("some text"); } var fnFromString = new Function("return " + fn.toString()); // function anonymous() { return function() { ... } } fnFromString = fnFromString(); // function() { alert("some text"); }

and if you executing script immediately:

eval("(" + fn.toString() + ")()"); // fn executed.


  • How to emulate integrated numeric keypad cursor keys in linux
  • SSLRead() return error -9806/15958)
  • disablinging autorecover option for powepoint
  • Slow performance in hybrid AngularJS and Angular application in Safari
  • jQuery file download plugin
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • Unable to run SDL program in Eclipse but able to do so in Windows Explorer
  • Cursor in wrong place in contenteditable
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • if some function is not optimized does it mean that all functions where it is declared are not optim
  • Application bar icon text length
  • Why doesn't a local variable live long enough for thread::scoped?
  • Synchronize windows folders
  • configure: error: no acceptable C compiler found in $PATH
  • AppleScript : find open tab in safari by name and open it
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Eclipse CDT error: Unable to compile
  • Is playing sound in Javascript performance heavy?
  • Problem while Building a Setup Project for a windows Service?
  • Marklogic : Query response time is very high
  • Chrome doesn't support silverlight anymore? How to solve this?
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Using $this when not in object context
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Accessing IRQ description array within a module and displaying action names
  • Sony Xperia Z Tablet not found by adb
  • Perl system calls when running as another user using sudo
  • Javascript convert timezone issue
  • Why winpcap requires both .lib and .dll to run?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How to set the response of a form post action to a iframe source?
  • Getting Messege Twice Using IMvxMessenger
  • LevelDB C iterator
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • How can i traverse a binary tree from right to left in java?