将值绑定到脚本中提到的变量
我正在创建一个 Java 库的 GWT 版本,它支持 javax.script.ScriptEngine 通过 Javascript 动态评估函数,例如,
o => o % 2 == 0
在运行时,“o”的值是通过 javax.script.Bindings 定义的(当然,o =>
部分被删除了)。
问题是,如何在 GWT 中获得相同的效果?我使用本机函数
native Object nativeEval(String script) /*-{
return $wnd.eval(script);
}-*/
nativeEval("o % 2 == 0");
但是如何将值绑定到标识符“o”?
I'm creating a GWT version of a Java library which has support for the javax.script.ScriptEngine to evaluate functions dynamically via Javascript, e.g.,
o => o % 2 == 0
where at runtime, the value of "o" is defined via the javax.script.Bindings (the o =>
part is stripped of course).
The problem is, how can I get the same effect from within GWT? I use a native function
native Object nativeEval(String script) /*-{
return $wnd.eval(script);
}-*/
nativeEval("o % 2 == 0");
But how can I bind a value to the identifier "o"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
expressionThatUsesO
是"o % 2"
那么这相当于立即调用的全局函数。供参考,https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function :
If
expressionThatUsesO
is"o % 2"
then this is equivalent to a global function that is immediately calledFor reference, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function :
将值传递给标识符“o”:
然后调用它:
To pass values to the identifier "o" :
And then calling it :
我想我找到了一个解决方案:
这个想法是在一个通常可访问的对象(例如窗口)上设置名称-值对,并更改脚本以从中获取变量。为了允许可重入的调用,绑定存储在不断增加的序列号下。
I guess I found a solution:
The idea is to set the name-value pairs on a commonly accessible object, such as window and alter the script to get the variables from it. In order to allow a re-entrant capable call, the bindings are stored under a constantly increasing sequence number.