Evaluating functions for transfer in IE8

江枫思渺然 提交于 2019-12-24 05:59:46

问题


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: 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


回答1:


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.




回答2:


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.




回答3:


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.


来源:https://stackoverflow.com/questions/5605969/evaluating-functions-for-transfer-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!