计算函数
我正在尝试创建一个小型库,您可以发送一个对象(具有函数)和一些数据和参数,它会将对象、数据和参数发送到工作进程,然后工作进程将评估该对象以重建函数(从字符串到函数)
目前我有这个:
JSON.stringify(object, function(key, val) { if typeof val === 'function' { return val + '' } 返回值 });
该函数将字符串化一个包含函数的对象。
在我的工人中,我试图撤消这个过程。
我已经尝试过以下方法: 评估(对象)
当我遇到一个函数时,我还尝试通过尝试 eval() 来实现 JSON.parse 。
我也尝试过 eval("return " + object.function)
这可能吗?
I am trying to create a small library that you can send an object(that has functions) and some data, and args, and it will send the object, data, and args to a worker process who will then eval the object to reconstruct the functions(from strings to functions)
Currently I have this:
JSON.stringify(object, function(key, val) {
if typeof val === 'function' { return val + '' }
return val
});
This function will stringify an object including functions.
In my worker I am trying to undo this process.
I have tried the following:eval(object)
I have also tried implementing JSON.parse with trying to eval() each function when I come across one.
I have also tried eval("return " + object.function)
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想要:
您不能将 return 语句放在某个地方。
function() {}
被评估为函数声明,但由于没有名称而失败。由于要解析的数据代表一个函数表达式,例如var f = function() {}
,因此需要在其周围放置()
,以便将其计算为表达式。You probably want:
You cannot put a return statement just somewhere.
function() {}
is evaluated as a function declaration, which fails because it has no name. Since the data to be parsed represents a function expression, likevar f = function() {}
, you need to put()
around it, so that it is evaluated as an expression.