开发者

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 m开发者_如何学CyFunctionTransferString = 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: http://jsfiddle.net/bMjug/

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:

eval('['+myFunctionTransferString+'][0]');

But seriously don't. Never rely on the string representation of a function, it is not standardised and there are many browser differences.

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 hacked 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜