评估IE8中的传输功能 [英] Evaluating functions for transfer in IE8

查看:131
本文介绍了评估IE8中的传输功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过JSON传输JavaScript对象并传输它的功能。我找到了获取函数字符串并传输它们的工作版本。然后我可以再次评估它们。

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();

这里我有一个jsfiddle: http://jsfiddle.net/bMjug/

Here i have a jsfiddle for this: http://jsfiddle.net/bMjug/

这适用于Firefox,Chrome和Safari,因为它应该(但你可以猜测不是微软软件称为Internet Explorer的那个伟大的地方)。

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

在我要评估函数的行中,我收到消息 fn在IE8中为null或不是对象

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

实际上我找到了一个解决方案,但我真的没有像这个解决方案。如果我把变量声明放入String我正在评估和删除parantheses,因为我不再期待一个可以做我想要的对象:

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?

提前致谢

推荐答案

对于它的价值,问题是由JScript错误地解释这个问题引起的:

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

(function x() {})

作为FunctionDeclaration而不是FunctionExpression,所以做一个statement-eval而不是表达式评估。与 {} 没有包装括号的对象文字类似。您可以通过更明确地将其推送到解析表达式来解决它,例如:

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.

即使函数分解是可靠的,也无法有效地保留函数,因为函数的功能远远多于其源的文本表示。它包含的闭包无法表示,闭包在现实世界的JavaScript中越来越常见。

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.

我担心没有廉价的方法来序列化/重新通常实例化JavaScript对象。 JSON子集是可靠的子集,其余的你必须编写自己的ad hoc序列化格式。

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.

这篇关于评估IE8中的传输功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆